POJ 1318 Word Amalgamation (字符串 STL大水)

Word Amalgamation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 8665   Accepted: 4172

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

题目链接:http://poj.org/problem?id=1318

题目大意:给一个字典,"XXXXXX"结束,再输入一些打乱的字母,"XXXXXX"结束,现在重排这些字母,问重排后的单词是否出现在字典中,出现就输出,可能有不同多个

题目分析:因为单词长度最大为6,直接next_permutation,字典用set存,0ms

代码:

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

int main()
{
    int cnt = 0;
    string s;
    set <string> st;
    set <string> :: iterator it;
    while(cin >> s)
    {
        if(s == "XXXXXX")
        {
            cnt ++;
            continue;
        }
        if(cnt == 2)
            break;
        if(cnt == 0)
            st.insert(s);
        else if(cnt == 1)
        {
            bool flag = false;
            sort(s.begin(), s.end());
            do
            {
                it = st.find(s);
                if(it != st.end())
                {
                    flag = true;
                    cout << s << endl;
                }
            }while(next_permutation(s.begin(), s.end()));
            if(!flag)
                printf("NOT A VALID WORD\n");
            printf("******\n");
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-11-10 00:15:52

POJ 1318 Word Amalgamation (字符串 STL大水)的相关文章

poj 1318 Word Amalgamation

Word Amalgamation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9968   Accepted: 4774 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

POJ 1318 Word Amalgamation (简单题)

[题意简述]:首先第一串"XX--"之前的是所谓的字典,然后在它之后的就是我们要查的字串.现在让我们逐个求出这些要查的字串排列后和字典中的字串相同的有哪些,输出出来,没有的话就输出:NOT AVALLID WORD [分析]:理解很简单,我们只需分别对字典中的这些字符串,和我们要查的这些字符串,每一个都排序,然后按位比较,每一位都是相同的那就输出出来就好了. #include<iostream> using namespace std; int n; char word[1

POJ 1318 Word Amalgamation结题报告

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

[POJ 1204]Word Puzzles(Trie树暴搜)

Description Word puzzles are usually simple and very entertaining for all ages. They are so entertaining that Pizza-Hut company started using table covers with word puzzles printed on them, possibly with the intent to minimise their client's percepti

POJ 1035 Spell Check 字符串处理

被这样的题目忽悠了,一开始以为使用Trie会大大加速程序的,没想到,一不小心居然使用Trie会超时. 最后反复试验,加点优化,终于使用Trie是可以过的,不过时间大概难高于1500ms,一不小心就会超时. 看来这是一道专门卡Trie的题目,只好放弃不使用Trie了. 也得出点经验,如果字符串很多,如本题有1万个字符串的,那么还是不要使用Trie吧,否则遍历一次这样的Trie是十分耗时的,2s以上. 于是使用暴力搜索法了,这里的技巧是可以使用优化技术: 1 剪枝:这里直接分组搜索,分组是按照字符串

hdu1113 Word Amalgamation(超详细解释--map和string的运用)

转载请注明出处:http://blog.csdn.net/u012860063天资 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1113 来吧!!欢迎"热爱编程"的同学报考杭电,期待你加入"杭电ACM集训队"! 7月22-8月21多校联合训练期间,会根据实际负载关闭部分模块,若有不便,请谅解~ Word Amalgamation Time Limit: 2000/1000 MS (Java/Others)    Me

POJ 1204 Word Puzzles AC自动机题解

AC自动机的灵活运用,本题关键是灵活二字. 因为数据不是很大,时间要求也不高的缘故,所以本题有人使用暴力法也过了,有人使用Trie也过了. 当然有人使用AC自动机没AC的,在讨论区里喊AC自动机超时的,那是因为不会灵活运用,或者是硬套模板的,AC了速度也不会快. 给出本人的算法思路: 1 把需要查找的关键字建立Trie, 然后构造AC自动机 2 查找的时候分八个方向查找,比如棋盘是board[N][M],那么就可以循环i(0->N-1),然后每次把board[i]当做一个文本,做过HDU的key

POJ 2049 Finding Nemo 优先队列 STL

题目链接:http://poj.org/problem?id=2049 题目利用了<海底总动员>的情节,小丑鱼尼莫迷路了,他老爸去营救他便是题意. 题目给出了这样的地图,说是假设地图由墙和门组成,忽略墙的厚度,地图上有门,没有墙的地方是可以自由行动的问可以经过最少多少道门便可以营救到尼莫. 这个题给的数据是墙的交点为整数点,但鱼爸爸实在非墙的地方自由移动. 因此,这个题有两个难点: 1.如果建图保存地图 2.如何在地图上遍历 由于题目是给出一个点(x,y),来表示一段墙 我便用一对X,Y来表示

Uva 642 - Word Amalgamation sort qsort

 Word Amalgamation  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