配置文件恢复(欢迎讨论)

描述:

有6条配置命令,它们执行的结果分别是:

命令执行

resetresetwhat

resetboardboardfault

boardaddwheretoadd

boarddeletnoboardatall

rebootbackplaneimpossible

backplaneabortinstallfirst

heheunkowncommand

注意:hehe不是命令。

为了简化输入,方便用户,以“最短唯一匹配原则”匹配:

1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:resetwhat;输入:res,根据该规则,匹配命令reset,执行结果为:resetwhat;

2、若只输入一字串,但本条命令有两个关键字,,则匹配失败。例如输入:reb,可以找到命令rebootbackpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unkowncommand

3、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果仍不唯一,匹配失败。例如输入:rb,找到匹配命令resetboard,执行结果为:boardfault。

4、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果唯一,匹配成功。例如输入:ba,无法确定是命令boardadd还是backplaneabort,匹配失败。

5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:boa,确定是命令boardadd,匹配成功。

6、若匹配失败,打印“unkonwcommand”

题目类别:字符串

难度:高级

运行时间限制:10Sec

内存限制:128MByte

阶段:入职前练习

输入:

多行字符串,每行字符串一条命令

输出:

执行结果,每条命令输出一行

样例输入:resetreset boardboard addboard deletreboot backplanebackplane abort

样例输出:reset whatboard faultwhere to addno board at allimpossibleinstall first

代码提交有一组数据没过,test7不知道是什么测试数据,希望各位网友批评指正~(待完善)

#include<iostream>#include<string>using namespace std;/*有6条配置命令,它们执行的结果分别是: 命 令 执 行 reset reset what reset board board fault board add where to add board delet no board at all reboot backplane impossible backplane abort install first he he unkown command */static string comm1[]={"reset","reset what"};//0,2static string comm2[]={//5,2"reset board#board fault","board add#where to add","board delet#no board at all","reboot backplane#impossible","backplane abort#install first"};static string comm3[]={"board fault","where to add","no board at all","impossible","install first"};int main(){string a[3];int i,j,k,secstart,flag,comm2black;int ok[5]={0};char b[20];//getline(cin,a);while(cin.getline(b,20)){if(strlen(b)==0)continue;//cout<<b<<endl;flag=0;memset(ok,0,sizeof(ok));for(i=0;i<strlen(b);i++){if(b[i]==' '){flag++;if(flag>1)break;secstart=i+1;}}//cout<<strlen(b)<<" "<<flag<<" "<<secstart<<endl;if(flag>1){cout<<"unkown command"<<endl;}else if(flag==0){for(i=0;i<strlen(b);i++)if(b[i]!=comm1[0][i]){cout<<"unkown command"<<endl;break;}if(i==strlen(b)) cout<<comm1[1]<<endl;}else {for(j=0;j<5;j++){for(i=0;b[i]!=' ' && comm2[j][i]!=' ';i++){if(b[i]!=comm2[j][i])break;}if(b[i]==' ')ok[j]=1;}for(j=0;j<5;j++){if(ok[j]==1){ok[j]=0;for(i=0;comm2[j][i]!=' ';i++);comm2black=i+1;for(i=secstart,k=comm2black;i<strlen(b) && comm2[j][k]!='#';i++,k++){if(b[i]!=comm2[j][k])break;}if(i==strlen(b))ok[j]=1;}}//for(i=0;i<5;i++)//cout<<ok[i]<<" ";//cout<<endl;secstart=0;for(i=0;i<5;i++){if(ok[i]==1) flag=i;secstart+=ok[i];}//cout<<flag<<" "<<secstart<<endl;if(secstart!=1) cout<<"unkown command"<<endl;else cout<<comm3[flag]<<endl;}}return 0;}

找一个让心里安静和干净的地方,

配置文件恢复(欢迎讨论)

相关文章:

你感兴趣的文章:

标签云: