题解 | #配置文件恢复#
配置文件恢复
http://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
1.先提取输入子串
2.匹配:只有一个串的时候看能不能匹配reset,
有两个串时循环剩下的五个命令,
符合条件:
com[i].find(s[0])==0&&com[i].find(s[i],com[i].find(" "))-com[i].find(" ")==1
有一条命令符合条件即输出,多条即失败
/*HJ66 配置文件恢复
命 令 执 行
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
*/
#include<iostream>
using namespace std;
string com[6]={ "reset",
"reset board",
"board add",
"board delete",
"reboot backplane",
"backplane abort"};
string act[6]={ "reset what",
"board fault",
"where to add",
"no board at all",
"impossible",
"install first"};
void match(string s[],int num)//字符串数组,数组长度
{
int count=0,pos=-1;
if(num==0&&s[0][0])//至少有一个字母
{
if(com[0].find(s[0])==0)
cout<<act[0]<<endl;
else cout<<"unknown command"<<endl;
}
else if(num==1)
{
for(int i=1;i<6;i++)
{
if(com[i].find(s[0])==0&&com[i].find(s[1],com[i].find(' '))-com[i].find(' ')==1)//第二个串的首字符在空格后面
{
count++;
pos=i;
}
}
if(count!=1) cout<<"unknown command"<<endl;
else cout<<act[pos]<<endl;
}
}
int main()
{
string a;
while(getline(cin, a))
{
int t=0;
string s[2]={""};
for(int i=0;i<a.length();i++)
{
if(a[i]==' ')
{
t++;
i++;
}
s[t]+=a[i]; //提取输入子串
}
match(s,t);
}
}