魔咒词典 为何总是WA?

helloDesword 2014-07-10 09:33:55
直接上题与链接。
http://acm.hdu.edu.cn/showproblem.php?pid=1880

Input
首先列出词典中不超过100000条不同的魔咒词条,每条格式为:
[魔咒] 对应功能

其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。
词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。

Output
每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”

我的方法中,
1、使用两个数组,分别存储按魔咒字典顺序排列和按对应功能字典顺序排列的的字符串,
2、根据出现的字符串类型,使用不同的搜索依据,使用的方法都是折半查找方法。

问题就是,始终在OJ中都是WA, 不太明白是哪个位置输出不对了。
麻烦各位帮帮忙~ 感谢各位~

测试数据:
Sample Input
[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


Sample Output
light the wand
accio
what?
what?

以下是代码:

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std;
typedef struct
{
char magic[25];
char func[85];
}NODE;


NODE words[100000+3];
NODE words2[100000+3];


bool cmp1(NODE a ,NODE b)
{
return strcmp(a.magic, b.magic) <0;
}

bool cmp2(NODE a,NODE b)
{
return strcmp(a.func, b.func) < 0;
}

//折半查找 搜索字符串
void search(char s[], int l , int r , int tag)
{
if( l > r)
{
printf("what?\n");
return ;
}
int m = (l+r)/2;
//magic --> fun
if( tag == 1)
{
if( strcmp(words[m].magic , s) < 0 )
search(s, m+1, r , tag);
else if(strcmp(words[m].magic , s) > 0 )
search(s, l , m-1, tag);
else
printf("%s\n",words[m].func);
}
else
{
if( strcmp(words2[m].func, s ) < 0)
search(s, m+1, r, tag);
else if(strcmp( words2[m].func, s) > 0 )
search(s, l , m-1, tag);
else
printf("%s\n",words2[m].magic);
}
}


int main()
{
char in[120];
char deal[120];
int t=0;
while( gets(in) && strcmp(in, "@END@") != 0)//
{
// if not, there will be a '\n' rest. and the result of 'scanf' will be 0
//fflush(stdin);
sscanf(in,"%[^]]",deal);
strcpy(words[t].magic, deal+1);
strcpy(words[t].func, in+strlen(deal)+2);
strcpy(words2[t].magic, deal+1);
strcpy(words2[t++].func, in+strlen(deal)+2);
}
//排序
sort(words, words+t,cmp1);
sort(words2,words2+t, cmp2);

int i,n;
scanf("%d",&n);
fflush(stdin);
while(n--)
{
gets(in);
if( in[0] == '[')
{
in[strlen(in)-1] = '\0';
search(in+1, 0,t-1,1);
}
else
{
search(in, 0,t-1,2);
}
}
return 0;
}
...全文
324 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
helloDesword 2014-07-21
  • 打赏
  • 举报
回复
由于hdu,zoj都是linux的编译系统,所以不支持。 相关解答如下: C标准(ISO/IEC 9899:1999 standard)规定fflush(stdin)操作是未定义的<参看《ISO/IEC 9899:1999 standard》p270>;。也就是说不一定能实现刷新功能,但有的编译器可能不遵循标准,对fflush(stdin)操作不予警告,并且有时可能产生正确的结果,但最好不要这样使用。 VC上面使用fflush()可以成功, 而在gcc上面使用fflush()却不能成功。 以下是 C99 对 fflush 函数的定义: int fflush(FILE *stream); 如果 stream 指向输出流或者更新流(update stream), 并且这个更新流最近执行的操作不是输入, 那么 fflush 函数将把这个流中任何待写数据传送至宿主环境(host environment)写入文件。 否则,它的行为是未定义的。 原文如下: int fflush(FILE *stream); If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined. 其中,宿主环境可以理解为操作系统或内核等。 由此可知,如果 stream 指向输入流(如 stdin),那么 fflush 函数的行为是不确定的。故而使用 fflush(stdin) 是不正确的,至少是移植性不好的。
helloDesword 2014-07-19
  • 打赏
  • 举报
回复
引用 12 楼 logiciel 的回复:
5楼代码改以下3行可AC:
  getchar();//改 fflush(stdin);
        printf("%s\n",(*q).func); //改 printf("%s,\n",(*q).func);
        printf("%s\n",(*q).magic); //改 printf("%s,\n",(*q).magic);
哎呀,真的可以ac了。后面两个我晓得,第一个是为什么呀。 getchar();//改 fflush(stdin); 编译器不支持?
logiciel 2014-07-19
  • 打赏
  • 举报
回复
5楼代码改以下3行可AC:
  getchar();//改 fflush(stdin);
        printf("%s\n",(*q).func); //改 printf("%s,\n",(*q).func);
        printf("%s\n",(*q).magic); //改 printf("%s,\n",(*q).magic);
helloDesword 2014-07-16
  • 打赏
  • 举报
回复
引用 10 楼 zhao4zhong1 的回复:
应判断是否最后一个元素,是:\n;否:,
不太明白, 题目中对于答案最后都是没有标点的。 只有两种。 答案 what?
赵4老师 2014-07-16
  • 打赏
  • 举报
回复
应判断是否最后一个元素,是:\n;否:,
helloDesword 2014-07-16
  • 打赏
  • 举报
回复
引用 8 楼 logiciel 的回复:
以下2行多输出了逗号: printf("%s,\n",(*q).func); printf("%s,\n",(*q).magic);
感谢哈~ 但是我去了逗号之后,还是wrong的。
helloDesword 2014-07-11
  • 打赏
  • 举报
回复
引用 6 楼 zhao4zhong1 的回复:
不要使用
……
噢,好的,谢谢赵老师。这种方法确实也能够从习惯上避免一些不必要的逻辑错误。 但是,在我的那个程序里面,应该不是这个问题吧? 手动测试对比答案都是一样的,但一直是WA。
logiciel 2014-07-11
  • 打赏
  • 举报
回复
以下2行多输出了逗号: printf("%s,\n",(*q).func); printf("%s,\n",(*q).magic);
赵4老师 2014-07-10
  • 打赏
  • 举报
回复
为什么不用bsearch函数呢?
iaccepted 2014-07-10
  • 打赏
  • 举报
回复
赵4老师 2014-07-10
  • 打赏
  • 举报
回复
不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
 if (条件1) break;
 //...
 if (条件2) continue;
 //...
 if (条件3) return;
 //...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
 a=fgetc(f);
 //...
 b=fgetc(f);//可能此时已经feof了!
 //...
}
而这样写就没有问题:
while (1) {
 a=fgetc(f);
 if (feof(f)) break;
 //...
 b=fgetc(f);
 if (feof(f)) break;
 //...
}
类似的例子还可以举很多。
helloDesword 2014-07-10
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
为什么不用bsearch函数呢?
上面的问题解决了,是提供给bsearch的key没有给正确,修改之后, 测试数据都是正确的。包括字符是否有多余空格,程序数据结构的范围我都检查过了。但是就是WA, 不太明白哪里不对劲了。 帮帮忙~谢谢~
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
	char magic[25];
	char func[85];
}NODE;


NODE words[100000+3]; 
NODE words2[100000+3];


int cmp1(const void *p, const void *q)
{
	return strcmp( (*(NODE *)p).magic, (*(NODE *)q).magic);
}

int cmp2(const void *p,const void *q)
{
	return strcmp( (*(NODE *)p).func, (*(NODE *)q).func );
}

int main()
{
	char in[120];
	char deal[120];
	int t=0;
	while( gets(in) && strcmp(in, "@END@") != 0)//
	{
		// if not, there will be a '\n' rest. and the result of 'scanf' will be 0
		//fflush(stdin);
		sscanf(in,"%[^]]",deal);		
		strcpy(words[t].magic, deal+1);
		strcpy(words[t].func, in+strlen(deal)+2);
		strcpy(words2[t].magic, deal+1);
		strcpy(words2[t++].func, in+strlen(deal)+2);		
	}	
	//排序 
	qsort( words, t, sizeof(NODE), cmp1);
	qsort( words2, t , sizeof(NODE), cmp2);

	int i,n;
	NODE *q,temp;
	scanf("%d",&n);
	fflush(stdin);
	while(n--)
	{
		gets(in);
		if( in[0] == '[')
		{
			in[strlen(in)-1] = '\0';
			strcpy(temp.magic,in+1);
			q = (NODE *)bsearch(&temp,words, t, sizeof(NODE), cmp1);	
			if( q== NULL)
			{
				printf("what?\n");
			}
			else
			{			
				printf("%s,\n",(*q).func);
							
			}
		}
		else
		{
			strcpy(temp.func, in);
			q = (NODE *)bsearch(&temp, words2, t ,sizeof(NODE), cmp2);
			if( q== NULL)
			{
				printf("what?\n");
			}
			else
			{
				printf("%s,\n",(*q).magic);
			}	
		}		

	}
	
	return 0;
}
helloDesword 2014-07-10
  • 打赏
  • 举报
回复
引用 1 楼 IAccepted 的回复:
可以看下我写的,http://blog.csdn.net/iaccepted/article/details/20851905
暴力查找可以的,那么可能就是我的二分法有问题了。 但是我始终找不到问题在哪里,下面使用了bsearch 也有点问题。
helloDesword 2014-07-10
  • 打赏
  • 举报
回复
引用 2 楼 zhao4zhong1 的回复:
为什么不用bsearch函数呢?
我改为使用bsearch函数了,但是由功能检索到魔咒的时候,就总是无法匹配成功,请问问题在哪呢?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
	char magic[25];
	char func[85];
}NODE;


NODE words[100000+3]; 
NODE words2[100000+3];


int cmp1(const void *p, const void *q)
{
	return strcmp( (*(NODE *)p).magic, (*(NODE *)q).magic);
}

int cmp2(const void *p,const void *q)
{
	return strcmp( (*(NODE *)p).func, (*(NODE *)q).func );
}

int main()
{
	char in[120];
	char deal[120];
	int t=0;
	while( gets(in) && strcmp(in, "@END@") != 0)//
	{
		// if not, there will be a '\n' rest. and the result of 'scanf' will be 0
		//fflush(stdin);
		sscanf(in,"%[^]]",deal);		
		strcpy(words[t].magic, deal+1);
		strcpy(words[t].func, in+strlen(deal)+2);
		strcpy(words2[t].magic, deal+1);
		strcpy(words2[t++].func, in+strlen(deal)+2);		
	}	
	//排序 
	qsort( words, t, sizeof(NODE), cmp1);
	qsort( words2, t , sizeof(NODE), cmp2);

	int i,n;
	NODE *q;
	scanf("%d",&n);
	fflush(stdin);
	while(n--)
	{
		gets(in);
		if( in[0] == '[')
		{
			in[strlen(in)-1] = '\0';
			q = (NODE *)bsearch(in+1,words, t, sizeof(NODE), cmp1);	
			//printf("%0x,",q);
			if( q== NULL)
			{
				printf("what?\n");
			}
			else
			{			
				printf("%s\n",(*q).func);
							
			}
		}
		else
		{
			q = (NODE *)bsearch(in, words2, t ,sizeof(NODE), cmp2);
			if( q== NULL)
			{
				printf("what?\n");
			}
			else
			{
				printf("%s\n",(*q).magic);
			}	
		}		

	}	
	return 0;
}

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧