题目描述:
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有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. poj3046

    dp,可以再优化. #include <iostream> #include <cstdio> #include <cstring> using namespace ...

  2. 【js】数组去重时间复杂度为n的方法

    # 时间复杂度O(n^2) function fn(arr) { return arr.filter((item, index, arr) => arr.indexOf(item) === in ...

  3. VC和MATLAB混合开发需要注意的一个问题

    作者:朱金灿 来源:http://blog.csdn.net/clever101 如果你的操作系统是64位操作系统,那么直接运行MATLAB的安装文件下的Setup.exe会默认安装的是64位的MAT ...

  4. No input file specified的解决方法

    (一)IIS Noinput file specified 方法一:改PHP.ini中的doc_root行,打开ini文件注释掉此行,然后重启IIS 方法二:请修改php.ini找到; cgi.for ...

  5. mybatis insert、update 、delete默认返回值解释与如何设置返回表主键

    在使用mybatis做持久层时,insert.update.delete,sql语句默认是不返回被操作记录主键的,而是返回被操作记录条数: 那么如果想要得到被操作记录的主键,可以通过下面的配置方式获取 ...

  6. MySQL出现GROUP BY clause错误解决办法

    #1)当mysql数据库出现如下错误:#which is not functionally dependent on columns in GROUP BY clause; this is incom ...

  7. Eclipse Java类编辑器里出现乱码的解决方案

    如图:在Java Class编辑器里出现的这种乱码,非常烦人. 解决方案:Windows->Preference->General->Appearance, 在里面将Theme设置成 ...

  8. x+2y+3z=n非负整数解

    #include <iostream> #include <string.h> #include <stdio.h> using namespace std; ty ...

  9. Android 使用 adb命令 远程安装apk

    Android 使用 adb命令 远程安装apk ./adb devices 列出所有设备 ./adb connect 192.168.1.89 连接到该设备 ./adb logcat 启动logca ...

  10. 洛谷 P1120 小木棍[数据加强版]

    这道题可能是我做过的数据最不水的一道题-- 题目传送门 这题可以说是神剪枝,本身搜索并不算难,但剪枝是真不好想(好吧,我承认我看了题解)-- 剪枝: 用桶来存储木棍 在输入的时候记录下最长的木棍和最短 ...