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. [LintCode笔记了解一下]39.恢复旋转排序数组

    思路: 1.需要O(n)的事件复杂度,所以多次循环不考虑 2.四步翻转法 -第一步,找到数组里最小的那个数字,因为是旋转排序数组,所以只要找到某个位置arr[i]>arr[i+1]的话,就找到了 ...

  2. Tomcat之——内存溢出设置JAVA_OPTS

    答案1设置Tomcat启动的初始内存其初始空间(即-Xms)是物理内存的1/64,最大空间(-Xmx)是物理内存的1/4.可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置三.实例,以下 ...

  3. Go 的垃圾回收机制在实践中有哪些需要注意的地方(转)

    在网上看到一篇非常好的文章http://www.zhihu.com/question/21615032,转载如下: go的gc还不完善但也不算不靠谱,关键看怎么用,尽量不要创建大量对象,也尽量不要频繁 ...

  4. C#委托同步异步说明,并比较control调用Invoke和BeginInvoke的异同

    一.委托的同步和异步: 1.同步 使用Invoke调用同步,或直接写fun1("func"),在fun1.Invoke这一步会明显的阻塞线程 使用: static void Mai ...

  5. CentOS 系统管理与yum软件仓库搭建

    重启 reboot shutdown -r now init 6 关闭 init 0 shutdown -h now shutdown -h 20:25 #8点25关机查看内存 free CPU利用率 ...

  6. SQL Server乱码处理(ASCII)

    CREATE FUNCTION [dbo].[RegexReplace] ( @string VARCHAR(MAX), --被替换的字符串 @pattern VARCHAR(255), --替换模板 ...

  7. Oracle下载及安装

    Oracle   下载及安装 一.官方下地址:   http://www.oracle.com/technetwork/database/enterprise-edition/downloads/in ...

  8. 十二、Nodejs 包与 NPM 第三方模块安装 package.json 以及 CNPM

    1. 包 Nodejs 中除了它自己提供的核心模块外,我们可以自定义模块,也可以使用第三方的模块.Nodejs 中第三方模块由包组成,可以通过包来对一组具有相互依赖关系的模块进行统一管理. 在 Nod ...

  9. redis可视化辅助工具

    安装链接: http://docs.redisdesktop.com/en/latest/quick-start/ 图标

  10. Django + Gunicorn + Nginx 部署 Ubuntu 服务器

    Django + Gunicorn + Nginx 部署服务器 获取腾讯云 root权限 本人的服务器使用的是腾讯云,腾讯云默认是没有开放 root 用户的,我们来创建 root 用户. 创建 roo ...