poj 1318 Word Amalgamation

简介:

这种字符串的题一定要仔细,不然很容易WA。。。

我开始的一种方法不是处理dictionary和sortDis,而是每次都搜索一次,到现在我都还没找到为什么错了。。。

后一种代码就是先处理dictionary和sortDis,之后就容易处理了


AC的代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

char dictionary[102][10];		//1~100词,每个词1~6字母
char sortDic[102][10];			//排序以后的字典
int dicNum;						//字典中词的个数

int main()
{
	//freopen("test.txt","r",stdin);

	char tmp[7];
	int i;
	//输入字典,从下标0开始		ok
	for(i=0; ;i++)
	{
		scanf("%s",tmp);
		if(strcmp(tmp,"XXXXXX")==0)
			break;
		
		strcpy(dictionary[i],tmp);
	}
	dicNum=i;

	//处理dictionary和sortDic
	int j;
	for(i=0;i<dicNum;i++)
		for(j=0;j<dicNum;j++)
			if(strcmp(dictionary[i],dictionary[j])<0)
			{
				strcpy(tmp,dictionary[j]);
				strcpy(dictionary[j],dictionary[i]);
				strcpy(dictionary[i],tmp);
			}//字典顺序已经排好

	//test
	/*for(i=0;i<dicNum;i++)
		printf("%s\n",dictionary[i]);*/

	for(i=0;i<dicNum;i++)	//排sortDic
	{
		strcpy(sortDic[i],dictionary[i]);
		sort(sortDic[i],sortDic[i]+strlen(sortDic[i]));
	}

	//输入待排序数
	while(scanf("%s",tmp))
	{
		if(strcmp(tmp,"XXXXXX")==0)
			return 0;

		sort(tmp,tmp+strlen(tmp));
		int count=0;
		for(i=0;i<dicNum;i++)
			if(strcmp(tmp,sortDic[i])==0)		//相等就标记一下
			{
				printf("%s\n",dictionary[i]);
				count++;
			}

		if(count==0)
		{
			printf("NOT A VALID WORD\n");
			printf("******\n");
			continue;
		}

		printf("******\n");
	}

	return 0;
}


一直WA的代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std;

char dictionary[102][7];		//1~100词,每个词1~6字母
char sortDic[102][7];			//排序以后的字典
int dicNum;						//字典中词的个数
int flag[102];

int main()
{
	//freopen("test.txt","r",stdin);

	char tmp[7];
	int i;
	//输入字典,从下标1开始		ok
	for(i=1; ;i++)
	{
		scanf("%s",tmp);
		if(strcmp(tmp,"XXXXXX")==0)
			break;
		
		strcpy(dictionary[i],tmp);
		strcpy(sortDic[i],tmp);
		sort(sortDic[i],sortDic[i]+strlen(tmp));
	}
	dicNum=i-1;

	//test
	/*for(i=1;i<=dicNum;i++)
		printf("%s\n",dictionary[i]);*/

	//test
	/*for(i=1;i<=dicNum;i++)
		printf("%s\n",sortDic[i]);*/

	//输入待排序数
	while(scanf("%s",tmp))
	{
		if(strcmp(tmp,"XXXXXX")==0)
			return 0;

		//init flag
		memset(flag,0,sizeof(flag));

		sort(tmp,tmp+strlen(tmp));
		int count=0;
		for(i=1;i<=dicNum;i++)
			if(strcmp(tmp,sortDic[i])==0)		//相等就标记一下
			{
				flag[i]=1;
				count++;
			}

		if(count==0)
		{
			printf("NOT A VALID WORD\n");
			printf("******\n");
			continue;
		}

		//开始正式输出
		int pos=1;
		while(1)
		{
			strcpy(tmp,"zzzzzz");
			//tmp必然是选出来最靠前的
			for(i=1;i<=dicNum;i++)
			{
				if(flag[i]==1 && strcmp(tmp,dictionary[i])>0)		//tmp字典序靠后
				{
					strcpy(tmp,dictionary[i]);
					pos=i;
				}
			}
			if(count==0)
				break;

			else
			{
				printf("%s\n",tmp);
				count--;
				flag[pos]=0;		//改回去
			}
		}
		printf("******\n");
	}

	return 0;
}


相关文章
|
6月前
|
C++ 容器
POJ3096—Surprising Strings
POJ3096—Surprising Strings
|
人工智能
[LeetCode] Word Ladder II
This is a tough problem! I read some solutions from the web. This link provides a one-end BFS solution.
798 0
[LeetCode] Word Ladder
Well, this problem has a nice BFS structure. Let's see the example in the problem statement. start = "hit" end = "cog" dict = ["hot", "dot", "dog"...
656 0
[LeetCode] Word Break
The typical solution to this problem is to use Dynamic Programming. The state dp[i] represents whether s[0.
753 0
[LeetCode] Word Break II
Well, it seems that many people meet the TLE problem. Well, I use a simple trick in my code to aoivd TLE.
895 0