九度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,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- Django之Form组件整理
搬运自:http://www.cnblogs.com/haiyan123/p/7795771.html 一.Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验 ...
- 断言assert用法
本文转自:http://blog.jobbole.com/76285/ 这个问题是如何在一些场景下使用断言表达式,通常会有人误用它,所以我决定写一篇文章来说明何时使用断言,什么时候不用. 为那些还不清 ...
- link标签的media属性的用法
<link rel=stylesheet" type="text/css" href="print.css" media="scree ...
- android textview添加滚动条
给textview添加滚动条 方式一: xml代码: //设置滚动条的方向 android:scrollbars="vertical" java中设置 tView=(TextVie ...
- url post 请求方法
最近的项目是给手机app 提供方法. 因此 此方法可以进行接口测试 static class HttpClient { static CookieContainer cookies = new Coo ...
- python GIL锁问题
一.GIL是什么 官方解释: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple na ...
- COGS 2280. [HZOI 2015]树白黑
★★ 输入文件:B_Tree.in 输出文件:B_Tree.out 简单对比时间限制:2 s 内存限制:512 MB [题目描述] 给定一棵有根树,树根为1,一开始这棵树所有节点均为白 ...
- Mybatis-Generator逆向生成Po,Mapper,XMLMAPPER(idea)
前文有一篇手工生成的说明,地址: http://www.cnblogs.com/xiaolive/p/4874605.html, 现在这个补充一下在idea里面的自动版本的数据库逆向生成工具: 一.g ...
- .net MVC下跨域Ajax请求(CORS)
二.CROS (Cross-origin Resource Sharing) CROS相当于一种协议,由浏览器.服务端共同完成安全验证,进行安全的跨域资源共享.对于开发人员来说就跟在本站AJAX请求一 ...
- mask rcnn和roi-align
faster-rcnn的github源码中是round四舍五入 但kaiming he的ppt是直接取整 1.讲roi-align和roi-pooling区别并且详细阐述roi-align过程的博客: ...