题目描述:
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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次,开始的代码是这样的:
 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#define MAX 100009
#define LENM 22
#define LENF 82 char magic[MAX][LENM];
char fun[MAX][LENF];
char temp[LENF];
char temp2[LENF]; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
int n = ;
while(true) {
scanf("%s",temp);
if(strcmp(temp,"@END@") == ) {
break;
}
strncpy(magic[n], &temp[], strlen(temp)-);
getchar();
gets(fun[n]);
n++;
}
int N;
scanf("%d",&N);
getchar();
for(int i = ; i < N; i++) {
gets(temp);
if(temp[] == '[') {
strncpy(temp2, &temp[], strlen(temp)-);
bool flag = false;
for(int j = ; j < n; j++) {
if(strcmp(temp2,magic[j]) == ) {
puts(fun[j]);
flag = true;
break;
}
}
if(flag == false) {
puts("what?");
}
}
else {
bool flag = false;
for(int j = ; j < n; j++) {
if(strcmp(temp,fun[j]) == ) {
puts(magic[j]);
flag = true;
break;
}
}
if(flag == false) {
puts("what?");
}
} }
return ;
}

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

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string> #define MAX 100009
#define LENM 22
#define LENF 82 char magic[MAX][LENM];
char fun[MAX][LENF];
char temp[LENF]; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
int n = ;
gets(temp);
while(strcmp(temp,"@END@") != ) {
int i;
for(i = ; i < strlen(temp); i++) {
if(temp[i] == ']') {
break;
}
}
strncpy(magic[n], &temp[], i-);
magic[n][i-] = '\0';
strcpy(fun[n], &temp[i+]);
fun[n][strlen(temp) - i - ] = '\0';
n++;
gets(temp);
}
int N;
scanf("%d",&N);
getchar(); for(int i = ; i < N; i++) {
gets(temp);
if(temp[] == '[') {
strncpy(temp, &temp[], strlen(temp)-);
temp[strlen(temp)-] = '\0';
bool flag = false;
for(int j = ; j < n; j++) {
if(strcmp(temp,magic[j]) == ) {
puts(fun[j]);
flag = true;
break;
}
}
if(flag == false) {
puts("what?");
}
}
else {
bool flag = false;
for(int j = ; j < n; j++) {
if(strcmp(temp,fun[j]) == ) {
puts(magic[j]);
flag = true;
break;
}
}
if(flag == false) {
puts("what?");
}
} } return ;
}

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

 #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include<iostream> #define MAX 100009
#define LENM 22
#define LENF 82 using namespace std; string magic[MAX];
string fun[MAX];
string str,temp,temp2; int main(int argc, char const *argv[])
{
//freopen("input.txt","r",stdin);
int n = ;
cin.ignore();
getline(cin,str);
while (str!="@END@"){
int i = str.find("]");
temp = str.substr(,i-);
temp2 = str.substr(i+);
magic[n] = temp;
fun[n] = temp2;
getline(cin,str);
n++;
} int N;
cin>>N;
cin.ignore(); for(int i = ; i < N; i++) {
getline(cin,str);
int k = str.find("]");
if(k != -) {
str=str.substr(,k-);
bool flag = false;
for(int j = ; j < n; j++) {
if(str == magic[j]) {
cout << fun[j] << endl;
flag = true;
break;
}
}
if(flag == false) {
puts("what?");
}
}
else {
bool flag = false;
for(int j = ; j < n; j++) {
if(str ==fun[j] ) {
cout << magic[j] << endl;
flag = true;
break;
}
}
if(flag == false) {
puts("what?");
}
} } return ;
}

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

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

最新文章

  1. LLVM 笔记(三)—— 了解传统编译器设计
  2. HTML基本知识
  3. mvc多个按钮的提交方法
  4. git ssh key创建和github使用
  5. OpenStack在线迁移
  6. JAVA-应用easyui
  7. ORACLE触发器概述之【行触发器】【weber出品】
  8. 萧墙HTML5手机发展之路(51)——jquerymobile在提高页面访问速度
  9. 201521123112《Java程序设计》第13周学习总结
  10. 基于无域故障转移群集 配置高可用SQLServer 2016数据库
  11. ASP.NET Core框架揭秘(持续更新中…)
  12. EntityFramework优化:第一次启动优化
  13. IOS 常遇到的报错警告 以及 解决办法
  14. Assignments---(贪心)
  15. ajax return 的问题
  16. Spring 中 Bean 的生命周期
  17. c#通过反射移除所有事件
  18. numpy.meshgrid()
  19. python处理数据问题详解
  20. git源码阅读

热门文章

  1. git push 冲突
  2. Json字符串转excel表格文件
  3. [转+补]Android打包so后魅族5中安装运行崩溃问题的解决方法
  4. Notepad++设计Tab制表符为4个空格
  5. mysqldumpslow及explain使用简介
  6. windows中安装模拟器后修改模拟器中的hosts方法
  7. JS中的事件、事件冒泡和事件捕获、事件委托
  8. Spring对注解(Annotation)处理【转】
  9. bootstrap 翻页(对齐的链接)
  10. c# DateTime常用用法