配置文件恢复,有一个样例没过

 

描述

有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

 注意:he he不是命令。

为了简化输入,方便用户,以“最短唯一匹配原则”匹配:
1、若只输入一字串,则只匹配一个关键字的命令行。例如输入:r,根据该规则,匹配命令reset,执行结果为:reset what;输入:res,根据该规则,匹配命令reset,执行结果为:reset what;
2、若只输入一字串,但本条命令有两个关键字,则匹配失败。例如输入:reb,可以找到命令reboot backpalne,但是该命令有两个关键词,所有匹配失败,执行结果为:unkown command
3、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果仍不唯一,匹配失败。例如输入:r b,找到匹配命令reset board,执行结果为:board fault。

4、若输入两字串,则先匹配第一关键字,如果有匹配但不唯一,继续匹配第二关键字,如果唯一,匹配成功。例如输入:b a,无法确定是命令board add还是backplane abort,匹配失败。
5、若输入两字串,第一关键字匹配成功,则匹配第二关键字,若无匹配,失败。例如输入:bo a,确定是命令board add,匹配成功。
6、若匹配失败,打印“unkonw command”

 

知识点  
运行时间限制 0M
内存限制 0
输入

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

输出

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

样例输入

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
#include<iostream>
#include<vector>
#include<string.h>

#include<stdio.h>

using namespace std;

char execution[7][50] = {"reset what", "board fault", "where to add", "no board at all", "impossible", "install first", "unkown command"};

unsigned int num[7] = {1,2,2,2,2,2,0};
char command[6][2][50] = { {"reset", "xx"},\
                                {"reset", "board"},\
                                {"board", "add"},\
                                {"board", "delet"},\
                                {"reboot", "backplane"},\
                                {"backplane", "abort"}
                            };

bool compare(vector<char*> &v, int idx)
{
    //cout<<idx<<"idx"<<endl;
    if(v.size()==num[idx])
    {
        for(int i=0; i<num[idx]; i++)
        {
            int sz = strlen(v[i]);
            //cout<<v[i]<<"display"<<endl;
            for(int j=0; j<sz; j++)
            {
                if(v[i][j] != command[idx][i][j])
                {
                    //cout<<v[i][j]<<"_vs_"<<command[idx][i][j]<<endl;
                    return false;
                }
            }
        }
        return true;
    }
    else
    {
        //cout<<"size not compatible"<<endl;
        return false;
    }
}

void divide(char *buf)
{
    vector<char *> v;
    int last = 0;
    int siz = strlen(buf);
    for(int i=0; i<=siz; i++)
    {
        if(i==0)
        {
        }
        else
        {
            if(buf[i]!=' ' && buf[i-1]==' ')
            {
                last = i;
            }

            if( (buf[i]==' ' || buf[i]=='\0') && buf[i-1]!=' ')
            {
                char *tmp;
                tmp = new char [50];
                int count = 0;
                for(int j=last; j<i; j++)
                {
                    tmp[count] = buf[j];
                    count++;
                }
                tmp[count] = '\0';
                v.push_back(tmp);
            }
        }
    }

/*
    for(int i=0; i<v.size(); i++)
    {
        cout<<v[i]<<'_';
    }
    cout<<endl;
*/

    if(v.size() > 2)
    {
        cout<<execution[6]<<endl;//<<"xx";
        return ;
    }
    else
    {
        int howmany=0;
        int theone= 0;
        for(int i=0; i<6; i++)
        {
            if( compare(v, i) )
            {
                howmany++;
                theone = i;
            }
            else
            {
            }
        }
        if(howmany==1)
        {

            cout<<execution[theone]<<endl;
        }
        else
        {
            cout<<execution[6]<<endl;
        }
        return;
    }

}

void solve(char *buf)
{
    divide(buf);

}

bool allkong(char *buf)
{
    for(int i=0; buf[i]!='\0'; i++)
    {
        if(buf[i]!=' ')
            return false;
    }
    return true;
}

int main()
{
    char buf[100];

    while( cin.getline(buf,100)  )
    {
        if(strlen(buf)>0 )
            solve(buf);
    }

    return 0;
}

  

posted @ 2017-01-11 13:01  Hardsoftware  阅读(342)  评论(0编辑  收藏  举报