HJ66. 配置文件恢复
描述
有6条配置命令,它们执行的结果分别是:
命 令 | 执 行 |
reset | reset what |
reset board | board fault |
board add | where to add |
board delete | no board at all |
reboot backplane | impossible |
backplane abort | install first |
he he | unknown command |
注意:he he不是命令。
为了简化输入,方便用户,以“最短唯一匹配原则”匹配(注:需从首字母开始进行匹配):
1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:reset what;输入:res,根据该规则,匹配命令reset,执行结果为:reset what;
2、若只输入一字串,但匹配命令有两个关键字,则匹配失败。例如输入:reb,可以找到命令reboot backpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unknown command
4、若输入两字串,则先匹配第一关键字,如果有匹配,继续匹配第二关键字,如果唯一,匹配成功。例如输入:bo a,确定是命令board add,匹配成功。
5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:b addr,无法匹配到相应的命令,所以执行结果为:unknow command。
6、若匹配失败,打印“unknown command”
输入描述
多行字符串,每行字符串一条命令
输出描述
执行结果,每条命令输出一行
示例1
输入:
reset reset board board add board delet reboot backplane backplane abort
输出:
reset what board fault where to add no board at all impossible install first
C 解法, 执行用时: 1ms, 内存消耗: 344KB, 提交时间: 2021-03-27
#include <stdio.h> #include <string.h> char exe[7][50] = {"reset what", "board fault", "where to add", "no board at all", "impossible", "install first", "unknown command"}; int GetInput(char **str) { int i = 0, ret = -1, pos = 0; char cRawStr[100] = {0}; if (fgets(cRawStr, 99, stdin) != NULL) { ret = sscanf(cRawStr, "%s %s", str[0], str[1]); } // printf("%s: %s %s %s %d\n", __FUNCTION__, cRawStr, *str, *(str + 1), ret); return ret; } int ParseCmd(char **str, int i) { // printf("%s: %s %s %d\n", __FUNCTION__, str[0], str[1], i); int index = -1; if (i == 0) { index = 6; } else if (1 == i) { if (strncmp(str[0], "reset", strlen(str[0])) == 0) { index = 0; } } else { if (strncmp(str[0], "reset", strlen(str[0])) == 0) { if (strncmp(str[1], "board", strlen(str[1])) == 0) { if (-1 != index) { index = 6; } else { index = 1; } } } if (strncmp(str[0], "board ", strlen(str[0])) == 0) { if (strncmp(str[1], "add", strlen(str[1])) == 0) { if (-1 != index) { index = 6; } else { index = 2; } } else if (strncmp(str[1], "delete", strlen(str[1])) == 0) { if (-1 != index) { index = 6; } else { index = 3; } } } if (strncmp(str[0], "reboot ", strlen(str[0])) == 0) { if (strncmp(str[1], "backplane", strlen(str[1])) == 0) { if (-1 != index) { index = 6; } else { index = 4; } } } if (strncmp(str[0], "backplane ", strlen(str[0])) == 0) { if (strncmp(str[1], "abort", strlen(str[1])) == 0) { if (-1 != index) { index = 6; } else { index = 5; } } } } if (-1 == index) { index = 6; } return index; } int main(void) { int i = 0, index = -1; char *str[2] = {NULL ,NULL}; str[0] = (char *)calloc(20, sizeof(char)); str[1] = (char *)calloc(20, sizeof(char)); while ((i = GetInput(str)) && i > 0) { index = ParseCmd(str, i); printf("%s\n", exe[index]); } return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 344KB, 提交时间: 2020-12-20
#include<stdio.h> #include<string.h> #include<ctype.h> char *cmdh[5]={"reset","board","board","reboot","backplane"}; char *cmdl[5]={"board","add","delete","backplane","abort"}; char *cmdout[5]={"board fault", "where to add", "no board at all", "impossible", "install first" }; int main() { char cmdin[100]; while(gets(cmdin)) { int n,len_1,len= strlen(cmdin);; int flag=0,pos=0,cnt=0; int index_o = 0; for(int n=0;n<len;n++) { if(cmdin[n]==' ') { flag=1; pos=n; break; } } if(flag == 1) { for(int n=0;n<5;n++) { if(strncmp(cmdh[n],cmdin,pos)==0 && strncmp(cmdl[n],cmdin+pos+1,len-pos-1)==0) { cnt++; index_o = n; } } if(cnt == 1) printf("%s\n",cmdout[index_o]); else printf("unknown command\n"); } else{ if(strncmp("reset",cmdin,len)==0) { printf("reset what\n"); } else{ printf("unknown command\n"); } } } return 0; }