文章标题

来源:互联网 发布:eclipse打印mysql表名 编辑:程序博客网 时间:2024/05/16 13:58

近义词维护
描述 :
系统需要支持四条命令。
命令是一行字符串,第一个单词是命令关键词,后面可能有若干个参数,命令字和参数之间以空格隔离。具体如下:
1、设置2个单词为近义词:
SetSynonyms 单词1 单词2
SetSynonyms为命令关键词,单词1和单词2是参数,以空格隔开
如果命令成功,不输出。如果命令执行失败:
a.如果命令参数过多,输出:error:too many parameters
b.如果如果命令参数过少,输出:error : too few parameters
c.其他原因导致的失败,输出:false

2、查询2个单词是否为近义词:
IsSynonyms 单词1 单词2
IsSynonyms 为命令关键词,单词1和单词2是参数,以空格隔开
如果成功,输出true;如果命令执行失败:
a.如果命令参数过多,输出:error:too many parameters
b.如果如果命令参数过少,输出:error : too few parameters
c.其他原因导致的失败,输出:false

3、清除系统所有的近义词关系。
ClearRelations
该命令没有参数。如果有参数,输出:error : too many parameters
注意:近义词具有相互传递性,如果A和B为近义词,B和C是近义词,那么A、B、C都为近义词。

                     4、结束命令,后面的输入全部忽略,不再处理任何命令。                     End                     5、接收的其他命令。输出:error:The system does not support this command                 运行时间限制 : 无限制                      内存限制 : 无限制                         输入 :

字符串多行,每行一条命令
输出 :
字符串
样例输入 :
SetSynonyms designed1 designed2
SetSynonyms designed2 designed3
IsSynonyms designed1 designed3
SetSynonyms designed4 designed3 designed5
End
样例输出 :
true
error : too many parameters

#include <string>#include <vector>#include <cstdlib>#include <math.h>#include <algorithm>#include <iostream>#include<time.h>using namespace std;#define MAX 10000int main()  //华为OJ,8个例子错了7个 ORZ不知道为什么{       string A[MAX];    string B[MAX];    int flag[MAX];    int cnt = 0,cntb = 0,lab = 1;    getline(cin, A[cnt]);    while (A[cnt] != "End")    {        cnt++;        getline(cin, A[cnt]);    }    cnt++;    for (int i = 0; i < cnt-1; i++)//最后一个End,不要考虑    {        int loc6 = A[i].find_first_not_of(" ");        string sss = A[i].substr(loc6);        A[i] = sss;        int loc = A[i].find(" ");        if (loc == -1)//无参数        {            if (A[i] == "SetSynonyms" || A[i] == "IsSynonyms")            {                cout << "error:too few parameters" << endl;            }            else if (A[i] == "ClearRelations")            {                for (int j = 0; j < cntb; j++) flag[j]= 0;            }            else            {                cout << "error:The system does not support this command" << endl;            }        }        else        {            string ins = A[i].substr(0, loc);            string temp = A[i].substr(loc + 1);            if (ins == "SetSynonyms")            {                int loc5 = temp.find_first_not_of(" ");                string ss = temp.substr(loc5);                temp = ss;                int loc1 = temp.find(" ");                if (loc1 == -1)                {                    cout << "error:too few parameters" << endl;                }                else                {                    string t1 = temp.substr(0, loc1);                    string t2 = temp.substr(loc1 + 1);                    int loc2 = t2.find(" ");                    int tt1,tt2,f1=0,f2=0;                    if (loc2 == -1)//正好2参数                    {                        for (int s = 0; s < cntb; s++)                        {                            if (B[s] == t1 )                            {                                tt1 = flag[s];                                f1 = 1;                                break;                            }                        }                        for (int s = 0; s < cntb; s++)                        {                            if (B[s] == t2)                            {                                tt2 = flag[s];                                 f2 = 1;                                break;                            }                        }                        if (f1 && f2)                        {                            if (tt1 != tt2)                            {                                for (int s = 0; s < cntb; s++)                                {                                    if (flag[s] == tt1 || flag[s] == tt2)                                    {                                        flag[s] = lab++;                                    }                                }                            }                        }                        else if (f1)                        {                            B[cntb++] = t2;                            flag[cntb-1] = tt1;                        }                        else if (f2)                        {                            B[cntb++] = t1;                            flag[cntb-1] = tt2;                        }                        else                        {                            B[cntb++] = t1;                            flag[cntb-1] = lab;                            B[cntb++] = t2;                            flag[cntb-1] = lab++;                        }                                           }                    else                    {                        cout << "error:too many parameters" << endl;                    }                }            }            else if (ins == "IsSynonyms")            {                int loc5 = temp.find_first_not_of(" ");                string ss = temp.substr(loc5);                temp = ss;                              int loc1 = temp.find(" ");                if (loc1 == -1)                {                    cout << "error:too few parameters" << endl;                }                else                {                    string t1 = temp.substr(0, loc1);                    string t2 = temp.substr(loc1 + 1);                    int loc3 = t2.find(" ");                    int tt1, tt2, f1 = 0, f2 = 0;                    if (loc3 == -1)//正好2参数                    {                        for (int s = 0; s < cntb; s++)                        {                            if (B[s] == t1)                            {                                tt1 = flag[s];                                f1 = 1;                                break;                            }                        }                        for (int s = 0; s < cntb; s++)                        {                            if (B[s] == t2)                            {                                tt2 = flag[s];                                f2 = 1;                                break;                            }                        }                        if (f1 && f2 && tt1>0 && tt2>0 && tt1 == tt2)                        {                                                           cout << "true" << endl;                        }                        else                        {                            cout << "false" << endl;                        }                    }                    else                    {                        cout << "error:too many parameters" << endl;                    }                }            }            else if (ins == "ClearRelations")            {                cout << "error:too many parameters" << endl;            }            else             {                cout << "error:The system does not support this command" << endl;            }        }    }    return 0;}
0 0
原创粉丝点击