题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
题意先匹配第一关键字再匹配第二关键字容易误导,一起匹配就好了。
先判断第二关键字是否为空
#include <stdio.h> #include <string.h> #define SIZE 20 const char *result[7] = { "unknown command","reset what","board fault","where to add", "no board at all","impossible","install first" }; int match(char *arr); int main(void) { char arr[SIZE+2]; while(fgets(arr,SIZE+1,stdin)){ if(arr[strlen(arr) - 1] == '\n') arr[strlen(arr) - 1] = '\0'; printf("%s\n",result[match(arr)]); } return 0; } int match(char *arr) { char *first,*second,*pt; int ans = 0; pt = arr; while(*pt != ' ' && *pt != '\0') pt++; if(*pt = ' ') *pt++ = '\0'; first = arr; second = pt; if(*second == '\0'){ if(strstr("reset",first)) ans = 1; }else{ if(strstr("reset",first) && strstr("board",second)){ if(strstr("reboot",first) && strstr("backplane",second)) ans = 0; else ans = 2; }else if(strstr("board",first) && strstr("add",second)){ if(strstr("backplane",first) && strstr("abort",second)) ans = 0; else ans = 3; }else if(strstr("board",first) && strstr("delete",second)){ ans = 4; }else if(strstr("reboot",first) && strstr("backplane",second)){ ans = 5; }else if(strstr("backplane",first) && strstr("abort",second)){ ans = 6; } } return ans; }