九度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次,开始的代码是这样的:#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:魔咒词典的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- MapWindowsPoints函数使用
MapWindowPoints的百度解释: 函数功能:该函数把相对于一个窗口的坐标空间的一组点映射成相对于另一窗口的坐标空 的一组点. 函数原型:int MapWindowPoints(HWND ...
- eclipse版本要求修改
eclipse要求打开的是java1.6,而安装的是java1.7,这个时候需要修改配置 找到JAVA的安装路径, 点击前往-电脑-资源库-Java-javaVCirtualMachines-...- ...
- POJ 2152 Fire (树形DP,经典)
题意:给定一棵n个节点的树,要在某些点上建设消防站,使得所有点都能够通过某个消防站解决消防问题,但是每个点的建站费用不同,能够保证该点安全的消防站的距离上限也不同.给定每个点的建站费用以及最远的消防站 ...
- poj1595 水题
题意:输入n, 和c 统计1 - n 有多少个素数为cnt 若 2*c > cnt 则将素数全部输出 否则分支判断: 若cnt 为偶数,则从中心开始输出2*c 个 若cnt 为奇数,则从中 ...
- js 前端不调接口直接下载图片
// 下载图片 downPhoto (path) { this.downloadFiles(path) }, // 下载 downloadFiles (content) { console.log(c ...
- html备忘录
上传文件 <form action="/ajax/" method="post" enctype="multipart/form-data&qu ...
- 黑苹果10.10.3手动开启SSD的TIRM提高硬盘效率
黑苹果10.10.3手动开启SSD的TIRM提高硬盘效率 文章前言 其实开启TIRM的方法有很多,比如用Clover注入的方式或者用其他的工具来方便完成,但是10.10.3刚刚出来有些工具还没有更新的 ...
- 【OS_Linux】Linux中虚拟机的三种上网方式——桥接、NAT、Host-only
1.桥接 桥接方便做实验,配置ip方便.可以和局域网中的其他机器进行通信,也可以和公网进行通信.缺点是会占用主机所在局域网的一个ip. 2. NAT NAT模式下虚拟机可以和主机进行通信,可以上网,而 ...
- 前端上传控件plupload总结
plupload是一个单图和多图上传控件: 属性和方法介绍,参考以下博客: https://www.cnblogs.com/2050/p/3913184.html 这里直接贴出JS代码,细到爆的注释, ...
- NGINX宏观手记
一.这里的优化主要是指对nginx的配置优化,一般来说nginx配置文件中对优化比较有作用的主要有以下几项: nginx进程数,建议按照cpu数目来指定,一般跟cpu核数相同或为它的倍数. ``` w ...