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;
}
时间: 2024-10-25 13:56:28

poj 1318 Word Amalgamation的相关文章

UVa 642 Word Amalgamation:查字典&amp;amp;字符串排序

642 - Word Amalgamation Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=583 In millions of newspapers across the United States there is a word game called Jumble. The object of this game

HDOJ/HDU 1113 Word Amalgamation(字典顺序~Map)

Problem Description In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four wor

POJ题目分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

编码-poj 2058算法题Word Encoding完整代码

问题描述 poj 2058算法题Word Encoding完整代码 题目描述 In any language, certain combinations of letters do not appear (or at least appear so seldom that they can be considered non-existent). For instance, there are no English words containing the three letter combin

poj分类

初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      (4)递推.      (5)构造法.(poj3295)      (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996) 二.图算法:      (1)图的深度优先遍历和广度优先遍历.      (2)最短路径算法(dijkstra,bellman-ford

poj 1733 Parity game:带权并查集

链接: http://poj.org/problem?id=1733 题目: Description Now and then you play the following game with your friend. Your friend writes down a sequence consisting of zeroes and ones. You choose a continuous subsequence (for example the subsequence from the

poj 1639 Picnic Planning:最小度限制生成树

链接: http://poj.org/problem?id=1639 题目: Picnic Planning Time Limit: 5000MS     Memory Limit: 10000K Total Submissions: 7780     Accepted: 2726 Description The Contortion Brothers are a famous set of circus clowns, known worldwide for their incredible

POJ 1077 Eight:八数码问题

题目链接: http://poj.org/problem?id=1077 题目类型: 隐式图搜索 原题: The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all pac

UVa 755 / POJ 1002 487--3279 (排序)

755 - 487--3279 Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=696 http://poj.org/problem?id=1002 Businesses like to have memorable telephone numbers. On