九度oj 题目1029:魔咒词典

题目描述:
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。

    给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
输入:

    首先列出词典中不超过100000条不同的魔咒词条,每条格式为:

    [魔咒] 对应功能

    其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。
    词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。

输出:
    每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”
样例输入:
[expelliarmus] the disarming charm
[rictusempra] send a jet of silver light to hit the enemy
[tarantallegra] control the movement of one's legs
[serpensortia] shoot a snake out of the end of one's wand
[lumos] light the wand
[obliviate] the memory charm
[expecto patronum] send a Patronus to the dementors
[accio] the summoning charm
@END@
4
[lumos]
the summoning charm
[arha]
take me to the sky
样例输出:
light the wand
accio
what?
what?

这道题我提交了n次,开始的代码是这样的:
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <string>
 5 #define MAX 100009
 6 #define LENM 22
 7 #define LENF 82
 8  
 9 char magic[MAX][LENM];
10 char fun[MAX][LENF];
11 char temp[LENF];
12 char temp2[LENF];
13  
14 int main(int argc, char const *argv[])
15 {
16     //freopen("input.txt","r",stdin);
17     int n = 0;
18     while(true) {
19         scanf("%s",temp);
20         if(strcmp(temp,"@END@") == 0) {
21             break;
22         }
23         strncpy(magic[n], &temp[1], strlen(temp)-2);
24         getchar();
25         gets(fun[n]);
26         n++;
27     }
28     int N;
29     scanf("%d",&N);
30     getchar();
31     for(int i = 0; i < N; i++) {
32         gets(temp);
33         if(temp[0] == '[') {
34             strncpy(temp2, &temp[1], strlen(temp)-2);
35             bool flag = false;
36             for(int j = 0; j < n; j++) {
37                 if(strcmp(temp2,magic[j]) == 0) {
38                     puts(fun[j]);
39                     flag = true;
40                     break;
41                 }
42             }
43             if(flag == false) {
44                 puts("what?");
45             }
46         }
47         else {
48             bool flag = false;
49             for(int j = 0; j < n; j++) {
50                 if(strcmp(temp,fun[j]) == 0) {
51                     puts(magic[j]);
52                     flag = true;
53                     break;
54                 }
55             }
56             if(flag == false) {
57                 puts("what?");
58             }
59         }
60          
61     }
62     return 0;
63 }
64  

这段代码犯了两个错误,一是[魔咒]中,魔咒中可以有空格,而scanf(%s)会去掉空格,二是strncpy函数不会在字符串的末尾添加'',导致结果错误,修改后的代码如下:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <string>
 5 
 6 #define MAX 100009
 7 #define LENM 22
 8 #define LENF 82
 9 
10 
11 char magic[MAX][LENM];
12 char fun[MAX][LENF];
13 char temp[LENF];
14 
15 int main(int argc, char const *argv[])
16 {
17     //freopen("input.txt","r",stdin);
18     int n = 0;
19     gets(temp);
20     while(strcmp(temp,"@END@") != 0) {
21         int i;
22         for(i = 0; i < strlen(temp); i++) {
23             if(temp[i] == ']') {
24                 break;
25             }
26         }
27         strncpy(magic[n], &temp[1], i-1);
28         magic[n][i-1] = '';
29         strcpy(fun[n], &temp[i+2]);
30         fun[n][strlen(temp) - i - 2] = '';
31         n++;
32         gets(temp);
33     }
34         int N;
35         scanf("%d",&N);
36         getchar();
37 
38         for(int i = 0; i < N; i++) {
39             gets(temp);
40             if(temp[0] == '[') {
41                 strncpy(temp, &temp[1], strlen(temp)-2);
42                 temp[strlen(temp)-2] = '';
43                 bool flag = false;
44                 for(int j = 0; j < n; j++) {
45                     if(strcmp(temp,magic[j]) == 0) {
46                         puts(fun[j]);
47                         flag = true;
48                         break;
49                     }
50                 }
51                 if(flag == false) {
52                     puts("what?");
53                 }
54             }
55             else {
56                 bool flag = false;
57                 for(int j = 0; j < n; j++) {
58                     if(strcmp(temp,fun[j]) == 0) {
59                         puts(magic[j]);
60                         flag = true;
61                         break;
62                     }
63                 }
64                 if(flag == false) {
65                     puts("what?");
66                 }
67             }
68             
69         
70         }
71     
72     
73     return 0;
74 }

事实上,采用c++的cin和string会更方便

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <string>
 5 #include<iostream>
 6 
 7 #define MAX 100009
 8 #define LENM 22
 9 #define LENF 82
10 
11 using namespace std;
12 
13 string magic[MAX];
14 string fun[MAX];
15 string str,temp,temp2;
16 
17 int main(int argc, char const *argv[])
18 {
19     //freopen("input.txt","r",stdin);
20     int n = 0;
21     cin.ignore();
22     getline(cin,str);
23     while (str!="@END@"){
24         int i = str.find("]");
25         temp = str.substr(1,i-1);
26         temp2 = str.substr(i+2);
27         magic[n] = temp;
28         fun[n] = temp2;
29         getline(cin,str);
30         n++;
31     }
32     
33     int N;
34     cin>>N;
35     cin.ignore();
36     
37     for(int i = 0; i < N; i++) {
38             getline(cin,str);
39             int k = str.find("]");
40             if(k != -1) {
41                 str=str.substr(1,k-1);
42                 bool flag = false;
43                 for(int j = 0; j < n; j++) {
44                     if(str == magic[j]) {
45                         cout << fun[j] << endl;
46                         flag = true;
47                         break;
48                     }
49                 }
50                 if(flag == false) {
51                     puts("what?");
52                 }
53             }
54             else {
55                 bool flag = false;
56                 for(int j = 0; j < n; j++) {
57                     if(str ==fun[j] ) {
58                         cout << magic[j] << endl;
59                         flag = true;
60                         break;
61                     }
62                 }
63                 if(flag == false) {
64                     puts("what?");
65                 }
66             }
67             
68         }
69     
70     return 0;
71 }

 ------------------2016-9-17更新

现在考虑此题用map的话可能会更加方便

原文地址:https://www.cnblogs.com/jasonJie/p/5687153.html