九度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函数不会在字符串的末尾添加'\0',导致结果错误,修改后的代码如下:
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] = '\0'; 29 strcpy(fun[n], &temp[i+2]); 30 fun[n][strlen(temp) - i - 2] = '\0'; 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] = '\0'; 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
九度oj 题目1029:魔咒词典相关推荐
- 九度OJ 题目1179:阶乘
/********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1179:阶乘 * 来源:http://ac.jo ...
- 九度OJ 题目1069:查找学生信息 随笔
** 九度OJ 题目1069:查找学生信息 ** 题目描述如下: 输入N个学生的信息,然后进行查询. 输入 输入的第一行为N,即学生的个数(N<=1000) 接下来的N行包括N个学生的信息,信息 ...
- 九度OJ 题目1203:IP地址
/********************************* * 日期:2013-2-8 * 作者:SJF0115 * 题号: 九度OJ 题目1203:IP地址 * 来源:http://ac. ...
- 九度OJ—题目1032:ZOJ
题目描写叙述: 读入一个字符串.字符串中包括ZOJ三个字符,个数不一定相等,按ZOJ的顺序输出.当某个字符用完时,剩下的仍然依照ZOJ的顺序输出. 输入: 题目包括多组用例,每组用例占一行,包括ZOJ ...
- 九度oj 题目1411:转圈
题目描述: 在一个有向图有n个顶点(编号从1到n),给一个起点s,问从起点出发,至少经过一条边,回到起点的最短距离. 输入: 输入包括多组,每组输入第一行包括三个整数n,m,s(1<=n< ...
- 【WA】九度OJ题目1435:迷瘴
题目描述: 通过悬崖的yifenfei,又面临着幽谷的考验-- 幽谷周围瘴气弥漫,静的可怕,隐约可见地上堆满了骷髅.由于此处长年不见天日,导致空气中布满了毒素,一旦吸入体内,便会全身溃烂而死. 幸好y ...
- 九度oj 题目1354:和为S的连续正数序列
题目描述: 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久, ...
- 九度oj 题目1376:最近零子序列
题目描述: 给定一个整数序列,你会求最大子串和吗?几乎所有的数据结构与算法都会描述求最大子串和的算法.今天让大家来算算最近0子串和,即整数序列中最接近0的连续子串和.例如,整数序列6, -4, 5, ...
- 九度oj 题目1078:二叉树遍历
题目1078:二叉树遍历 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5326 解决:3174 题目描述: 二叉树的前序.中序.后序遍历的定义: 前序遍历:对任一子树,先访问跟,然后遍历 ...
- 九度oj 题目1374:所有员工年龄排序
题目描述: 公司现在要对所有员工的年龄进行排序,因为公司员工的人数非常多,所以要求排序算法的效率要非常高,你能写出这样的程序吗? 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入的第一行为一 ...
最新文章
- Elasticsearch的javaAPI之query dsl-queries
- R语言ggplot2包以及lattice包可视化方程、函数的曲线实战:function curve
- [BZOJ5292][BJOI2018]治疗之雨(概率DP+高斯消元)
- slf4j、jcl、jul、log4j1、log4j2、logback大总结
- 吴恩达 coursera AI 第三课总结+作业答案
- eclipse中Android模拟器常见的问题解决方法汇总
- Unable to execute dex: Multiple dex files define Lcom
- 新发现一款监控Linux集群sinfo
- leetcode 491. 递增子序列 思考分析
- STL容器及其简单应用(stack、priority_queue、vector、deuqe、list、map/multimap、set/multiset)
- 计算机二级C语言考的函数,【帮考网】2012计算机等级二级考试C语言:函数递归...
- java并发编程之Thread.sleep方法详解
- 刷屏!黄晓明杨颖官宣离婚,两人已无商业关联
- 这67款APP被下架了
- 东大OJ-1544: GG的战争法则
- 超强数据恢复软件【含注册码】
- html中用于排版标题标签是,web端测试也需要懂-HTML排版标签
- light动名词_英语语法大全之动名词
- Excel中的DATEDIF函数
- 小米手机是什么系统?
热门文章
- python乌龟吃鱼_Python面向对象练习-通过pygame改良乌龟吃鱼
- 解决winrm4j中文乱码问题和执行脚本文件
- 传送网SDN化再上台阶 中联通将全面开启T-SDN商用
- 惠普计算机图标不在桌面,惠普笔记本电脑桌面图标显示图标不正常如何还原 惠普笔记本电脑桌面图标显示图标不正常还原的方法...
- 删除word中自己插入的单行、双行线
- 英语cowbezoar牛黄CowBezoar单词
- 【社招】量化研究员(机器学习)-Akuna Capital -上海
- EVE-NG模拟器教程(二)——模拟器安装
- Python自动锁屏--window系统
- python判断闰年_python判断闰年