题目描述:
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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的话可能会更加方便

九度oj 题目1029:魔咒词典的更多相关文章

  1. 九度OJ 题目1384:二维数组中的查找

    /********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...

  2. hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人

    钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  3. 九度oj题目&amp;吉大考研11年机试题全解

    九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码).    http://ac.jobdu.com/problem.php?pid=11 ...

  4. 九度oj 题目1007:奥运排序问题

    九度oj 题目1007:奥运排序问题   恢复 题目描述: 按要求,给国家进行排名. 输入:                        有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ题目1105:字符串的反码

    tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...

  7. 九度oj题目1009:二叉搜索树

    题目描述: 判断两序列是否为同一二叉搜索树序列 输入:                        开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...

  8. 九度oj题目1002:Grading

    //不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...

  9. 九度OJ题目1003:A+B

    while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...

随机推荐

  1. iOS 通知、本地通知和推送通知有什么区别? APNS机制。

    本地/推送通知为不同的需要而设计.本地通知对于iPhone,iPad或iPod来说是本地的.而推送通知——来自于设备外部.它们来自远程服务器——也叫做远程通知——推送给设备上的应用程序(使用APNs) ...

  2. Selenium私房菜系列7 -- 玩转Selenium Server

    本篇主要是想更进一步介绍Selenium Server的工作原理,这次我们从Selenium Server的交互模式开始. 在<第一个Selenium RC测试案例>中,我们以命令“jav ...

  3. mysql IF语句使用

    类似于三元运算符 1)  IF(where,result1,result2) = where?result1:result2 例如 SELECT IF(1=1,1,2)    =>  1 2)  ...

  4. ftpclient 遇到的一些问题

    1. FTPFile[] files=ftpClient.listFiles(ftpDirectory); 没有数据 public static boolean ftpLogin(String ser ...

  5. 使用ABAP编程实现对微软Office Word文档的操作

    SAP ABAP里提供了一个标准的类CL_DOCX_DOCUMENT,提供了本地以".docx"结尾的微软Office word文档的读和写操作. 本文介绍了ABAP类CL_DOC ...

  6. python学习(day1)

    一.在这次实训之前,虽然听说过很多次python这种语言,但是从来没有真正去学习过,仅仅知道它是一种目前十分流行且功能非常强大的语言,可以方便快捷的实现很多功能.今天的课程带我了解了python,并且 ...

  7. 用valgrind检查内存问题

    Valgrind Valgrind作为一个免费且优秀的工具包,平时大部分人可能都是使用valgrind检测内存问题,如内存泄露,越界等. Valgrind工具包包含多个工具,如Memcheck,Cac ...

  8. C++函数调用过程深入分析<转>

    转自http://blog.csdn.net/dongtingzhizi/article/details/6680050 C++函数调用过程深入分析 作者:靠谱哥 微博:洞庭之子-Bing 0. 引言 ...

  9. LeetCode || 双指针 / 单调栈

    11. Container With Most Water 题意:取两根求最大体积 思路:使用两个指针分别指向头和尾,然后考虑左右两根: 对于小的那根,如果选择了它,那么能够产生的最大体积一定是当前的 ...

  10. ios之UIImageView

    UIImageView,顾名思义,是用来放置图片的.使用Interface Builder设计界面时,当然可以直接将控件拖进去并设置相关属性,这就不说了,这里讲的是用代码. 1.创建一个UIImage ...