hdu1113 Word Amalgamation(map 和 string 的运用)

本文详细解读了Word Amalgamation算法问题的输入、输出规范,并提供了利用map和string解决此类问题的代码示例。重点在于理解如何通过排序和映射数据结构,高效匹配并输出符合条件的单词。

 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113

 

 


来吧!!欢迎"热爱编程"的同学报考杭电,期待你加入“杭电ACM集训队”! 
7月22-8月21多校联合训练期间,会根据实际负载关闭部分模块,若有不便,请谅解~

Word Amalgamation

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


 
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 ******
 
 
Source
 
 
Recommend
Eddy

 

题意:

先给你一些单词作为字典,在给一系列的单词查找字典中是否有这些单词(注意查找的单词,一个单词中的字母顺序是可以变得,也就是说单词之间只要字母是一样的不用考虑顺序是否一样都要输出);

 

 

思路:

用map和string便很容易解决,先把字典存入map里,在逐一查找就OK,
查找的时候需要一点小小的操作,详见代码解释;

 

map的详细用法:http://blog.youkuaiyun.com/u012860063/article/details/24435211

 

代码如下:

 

#include <cstdio>
#include <map>
#include <string>
#include <iostream>
#include <algorithm> 
using namespace std;

int main()
{
	map<string,string>m;//定义map的变量和值都为string类型 
	string t, s;
	while(cin >> s)
	{
		if( s == "XXXXXX") 
		{	//注:string类型的是可以直接在字符串之间用"="或"=="进行赋值或判断的
			break;
		}
		t = s;
		sort(s.begin(),s.end());//直接对string类型的字符串用begin()和end()进行排序 
		m[t] = s;
	}
	while(cin >> s)
	{
		if( s == "XXXXXX")
		{
			break;
		}
		int flag = 0;//用于记录字典里是否有要查询的单词 
		sort(s.begin(),s.end());//直接对string类型的字符串用begin()和end()进行排序 
		map<string,string>::iterator it;
		for(it = m.begin(); it != m.end(); it++)
		{
			if(it->second == s)//it->second 为值,也就是map里第二个string的值 
			{
				cout<<it->first<<endl;//it->first 为索引键值,也就是map里第一个string的值 
				flag = 1;
			}
		}
		if(flag == 0)
		cout << "NOT A VALID WORD"<<endl; //和下面注释掉的等效 
		cout << "******"<<endl;
	//	cout << "NOT A VALID WORD\n"<<; 
	//	cout << "******\n"<<;
	}
	return 0;
}

 

 

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值