博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation【MAP+排序】
阅读量:5740 次
发布时间:2019-06-18

本文共 2744 字,大约阅读时间需要 9 分钟。

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9395   Accepted: 4498

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

tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX

Sample Output

score******refund******parttarptrap******NOT A VALID WORD******course******

Source

 >> 

问题链接:。

问题描述参见上文。

问题分析这是一个字典问题,自然用map来实现。问题在于还需要转个弯,不然会掉进陷阱里去的。

查字典问题,通常是单词按照字典顺序存放,然后将要查的单词拆成字母,按单词的字母顺序去查字典。然而要是这样做程序的逻辑就太零碎繁杂了。

于是,把单词中的字符排个顺序作为关键字,来查单词的话就方便了。需要注意的一点,不同的单词有可能具有相同的关键字。

程序说明这个程序的关键有容器类map的使用,算法库<algorithm>中函数sort()的使用。

这个程序未必是最佳做法,使用map<string vector<string>>作为字典也许是最佳的做法。

AC的C++语言程序如下:

/* HDU1113 POJ1318 UVA642 ZOJ1181 UVALive5328 Word Amalgamation */#include 
#include
#include
#include
using namespace std;int main(){ map
dic; string word, key; while(cin >> word && word != "XXXXXX") { key = word; sort(key.begin(), key.end()); dic[word] = key; } while(cin >> key && key != "XXXXXX") { sort(key.begin(), key.end()); bool flag = true; for(map
::iterator it=dic.begin(); it!=dic.end(); it++) { if(it->second == key) { cout << it->first << endl; flag = false; } } if(flag) cout << "NOT A VALID WORD" << endl; cout << "******" << endl; } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7564733.html

你可能感兴趣的文章
IntelliJ IDEA 连接数据库详细过程
查看>>
thymeleaf 学习笔记-基础篇
查看>>
PHP-X开发扩展
查看>>
android学习笔记——onSaveInstanceState的使用
查看>>
工作中如何做好技术积累
查看>>
怎么用sysLinux做U盘双PE+DOS??
查看>>
Spring Transactional
查看>>
shell脚本实例
查看>>
我的友情链接
查看>>
Windows Phone 7 隔离存储空间资源管理器
查看>>
Microsoft Excel 2000/2003修复工具
查看>>
apache安装报错undefined reference ssl
查看>>
关于爱情只有一句忠告
查看>>
CentOS 7下安装部署Oracle11g图文教程
查看>>
F#初学笔记06
查看>>
实战:将企业域名解析委派给企业DNS服务器
查看>>
在Lync 2013环境部署Office Web Apps
查看>>
微软大会Ignite,你准备好了么?
查看>>
读书笔记-高标管事 低调管人
查看>>
Master带给世界的思考:是“失控”还是进化
查看>>