Word Amalgamation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9794   Accepted: 4701

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
******

题目的意思是:给出一个以XXXXXX为结尾的字符串集作为字典,再输入一个以XXXXXX为结尾的字符串集作为要在字典中查找的目标字符串。判断目标字符串是否是由字典中的字符串经过变换得到的。

思路:按照字典序变换并记录 字符串集里面的每个字符串,对于目标字符串 也按字典序排列,并找出 与 排序后的目标字符串 相等的串存储其变换前对应字符串,按字典序排序后输出即可。

//poj1318
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std; #define N 105
#define M 10
char map[N][M];
char mark[N];
char s[N],ans[M];
int n,len;
bool vis[M],flag; void dfs(int step)
{
int i,j;
if(step == len)
{
ans[len] = '\0';//字符串结束符
for(j = ; j < n; ++j)
{
if(!strcmp(map[j],ans) && !mark[j])
{
mark[j] = ;
printf("%s\n",map[j]);
flag = ;
return ;
}
}
}
for(i = ; i < len; ++i)
{
if(!vis[i])
{
vis[i] = ;
ans[step] = s[i];
dfs(step+);
vis[i] = ;
}
}
} int cmp(const void * a,const void *b)
{
return *(char *)a - *(char *)b ;
} int main()
{
n = ;
while(scanf("%s",s)!=EOF)
{
if(!strcmp("XXXXXX",s))//这里的strcmp会按字典序比较
break;
else
strcpy(map[n++],s);
}
while(scanf("%s",s)!=EOF)
{
if(!strcmp("XXXXXX",s))
break;
len = strlen(s);
qsort(s,len,sizeof(s[]),cmp);//对字符串按字典序排序
memset(vis,,sizeof(vis));
memset(mark,,sizeof(mark));
flag = ;
dfs();
if(!flag)
printf("NOT A VALID WORD\n");
printf("******\n");
}
return ;
}

poj1318 Word Amalgamation 字符串排序(qsort)的更多相关文章

  1. ZOJ1181 Word Amalgamation 字符串 排序查找

    传送门:ZOJ1181  思路:自身排序来判断两个字符串拥有相同的字符.   #include<cstdio> #include<cstdlib> #include<io ...

  2. Word Amalgamation(枚举 + 排序)

    Word Amalgamation Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 373  Solved: 247 Description In mil ...

  3. hdu-----(1113)Word Amalgamation(字符串排序)

    Word Amalgamation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. Uva 642 - Word Amalgamation sort qsort

     Word Amalgamation  In millions of newspapers across the United States there is a word game called J ...

  5. hdu1113 Word Amalgamation(详解--map和string的运用)

    版权声明:本文为博主原创文章.未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/35338617 转载请注明出 ...

  6. python实验二:字符串排序

    ##统计word中的各个字符的出现的次数,并统计出所有前十名的字符使用次数 # -*- coding:utf-8 -*- word='''awfesdafhjkcasadckjsdackjsadvcn ...

  7. Trie树|字典树(字符串排序)

    有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...

  8. HDOJ.1113 Word Amalgamation(map)

    Word Amalgamation 点我挑战题目 点我一起学习STL-MAP 题意分析 给出字典.之后给出一系列======乱序======单词,要求你查字典,如过这个乱序单词对用有多个有序单词可以输 ...

  9. hdu - 1113 Word Amalgamation (stl)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 给定一个字典,然后每次输入一个字符串问字典中是否有单词与给定的字符串的所有字母一样(顺序可以打乱),按字典 ...

随机推荐

  1. DataTable 设置primarykey 后进行 Merge操作

    1.先查看概念 可以看highplayer博客 http://blog.csdn.net/highplayer/article/details/6613817 2. protected void st ...

  2. 第06章-渲染Web视图

    1. 理解视图解析 将控制器中请求处理的逻辑和视图中的渲染实现解耦是Spring MVC的一个重要特性.如果控制器中的方法直接负责产生HTML的话,就很难在不影响请求处理逻辑的前提下,维护和更新视图. ...

  3. URLRewrite 实现方法详解

    所谓的伪静态页面,就是指的URL重写,在ASP.NET中实现非常简单首先你要在你的项目里引用两个DLL:ActionlessForm.dll.URLRewriter.dll,真正实现重写的是 URLR ...

  4. 洛谷P3779 [SDOI2017]龙与地下城(概率论+Simpson+FFT)

    题面 传送门 题解 orz shadowice 正态分布 正态分布是随机变量\(X\)的一种概率分布形式.它用一个期望\(\mu\)和方差\(\sigma^2\)就可以描述,记为\(N(\mu,\si ...

  5. 【spring】bean加载顺序

    问题来源 有一个bean为A,一个bean为B.想要A在容器实例化的时候的一个属性name赋值为B的一个方法funB的返回值. 如果只是在A里单纯的写着: private B b; private S ...

  6. python3入门之字符串

    获得更多资料欢迎进入我的网站或者 csdn或者博客园 经过前面的介绍相信大家也对python有了一个初步的了解:本节主要介绍字符串,不管学习什么编语言字符串一定在其中扮演着重要的地位.本节主要讲解,字 ...

  7. 当AVPlayer在被释放之后,Player一直监听的时间没有被移除,提示错误的解决办法

    Xcode Consolu打印出来的提示: An instance 0x156608c0 of class AVPlayer was deallocated while key value obser ...

  8. SqlBulkCopy使用注意事项

    1. 有标识列的表 1.1 SqlBulkCopyOptions.KeepIdentity  必须设置!否则会出现复制过去的数据产生标识列发现变化的情况! 1.2 如果原表的标识列即为主键, 那按1. ...

  9. gym 102082G BZOJ4240 贪心+树状数组

    4240: 有趣的家庭菜园 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 756  Solved: 349[Submit][Status][Discu ...

  10. [国家集训队]部落战争 最大流 BZOJ2150

    题目描述 lanzerb的部落在A国的上部,他们不满天寒地冻的环境,于是准备向A国的下部征战来获得更大的领土. A国是一个M*N的矩阵,其中某些地方是城镇,某些地方是高山深涧无人居住.lanzerb把 ...