HDOJ 1113 Word Amalgamation

本文转载于:笑着走完自己的路csdn

Word Amalgamation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2818    Accepted Submission(s): 1354


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 words. Your task is to write a program that can unscramble words.
 

Input
The input contains four parts:

1. a dictionary, which consists of at least one and at most 100 words, one per line;
2. a line containing XXXXXX, which signals the end of the dictionary;
3. one or more scrambled `words' that you must unscramble, each on a line by itself; and
4. another line containing XXXXXX, which signals the end of the file.

All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X's.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique.
 

Output
For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line ``NOT A VALID WORD" instead. In either case, output a line containing six asterisks to signal the end of the list.
 

Sample Input
  
  
tarp given score refund only trap work earn course pepper part XXXXXX resco nfudre aptr sett oresuc XXXXXX
 

Sample Output
  
  
score ****** refund ****** part tarp trap ****** NOT A VALID WORD ****** course ******
 
 
这题看了好几天了,之前找到各种题解,有用map映射做的,有用qsotr的,大部分都是C++做法。本渣太弱,题解都看不懂。
还好看了振宇大神博客,他的方法简单易懂。   长见识了,原来sort还可以对字符串进行排序。
 
 
题目的意思是:给出一个以XXXXXX为结尾的字符串集作为字典,再输入一个以XXXXXX为结尾的字符串集作为要在字典中查找的目标字符串。判断目标字符串是否是由字典中的字符串经过变换得到的。
 
 
思路:按照字典序变换并记录 字符串集里面的每个字符串,对于目标字符串 也按字典序排列,并找出 与 排序后的目标字符串 相等的串存储其变换前对应字符串,按字典序排序后输出即可。
 
 
具体代码如下:
 
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct rec
{
	char st[10];
}num[110];
char s[110][10],cpy[110][10],str[10];

bool cmp(rec a,rec b)//利用结构体储存一个目标字符串在字典中有多个字符串对应的情况 
{
	return strcmp(a.st,b.st)<0;//该种情况下按照strcmp函数的升序输出 
}

int main()
{
	int n=0,i,t,sign;
	while(scanf("%s",s[n])&&strcmp(s[n],"XXXXXX")!=0)
	{
		strcpy(cpy[n],s[n]); 
		sort(cpy[n],cpy[n]+strlen(cpy[n]));//对字典中的字符串进行排序 
		n++;
	}
	while(scanf("%s",str)&&strcmp(str,"XXXXXX")!=0)
	{
		t=0;//记录字典中与目标字符串匹配字母的个数 
		sign=0;//标记是否有匹配字符串 
		sort(str,str+strlen(str));
		for(i=0;i<n;i++)
		{
			if(strcmp(str,cpy[i])==0)
			{
				sign=1;
				strcpy(num[t++].st,s[i]);//将查找到的字符串对应原字典中单词存入结构体 
			}
		}
		if(!sign)
		    printf("NOT A VALID WORD\n");
		else
		{
			sort(num,num+t,cmp);
			for(i=0;i<t;i++)
			   printf("%s\n",num[i].st);
		}
		printf("******\n");
	}
	return 0;
}

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5522 Accepted: 2789 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 words. Your task is to write a program that can unscramble words. Input The input contains four parts: 1) a dictionary, which consists of at least one and at most 100 words, one per line; 2) a line containing XXXXXX, which signals the end of the dictionary; 3) one or more scrambled 'words' that you must unscramble, each on a line by itself; and 4) another line containing XXXXXX, which signals the end of the file. All words, including both dictionary words and scrambled words, consist only of lowercase English letters and will be at least one and at most six characters long. (Note that the sentinel XXXXXX contains uppercase X's.) The dictionary is not necessarily in sorted order, but each word in the dictionary is unique. Output For each scrambled word in the input, output an alphabetical list of all dictionary words that can be formed by rearranging the letters in the scrambled word. Each word in this list must appear on a line by itself. If the list is empty (because no dictionary words can be formed), output the line "NOT A VALID WORD" instead. In either case, output a line containing six asterisks to signal the end of the list. Sample Input tarp given score refund only trap work earn course pepper part XXXXXX resco nfudre aptr sett oresuc XXXXXX Sample Output score ****** refund ****** part tarp trap ****** NOT A VALID WORD ****** course ****** Source Mid-Central USA 1998

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值