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. Smarty3——从配置文件获取的变量

    再使用配置变量前要 引入配置变量即:{$config_load file=‘file_path’}$marty3中可以从配置文件中 用 # 号包起来引用配置文件中的变量({#config_var_na ...

  2. 【linux命令】setterm控制终端属性命令(中英文)

    [linux命令]setterm控制终端属性命令(中英文) 2018年03月23日 17:13:44 阅读数:489 标签: linux 更多 个人分类: linux 摘自:https://blog. ...

  3. Part5核心初始化_lesson4---关闭中断

    1.关闭cpsr寄存器里面的I(中断)和F(快速中断)位: 2.设置中断屏蔽寄存器. 针对2440: 这是中断处理过程,当有中断源(没有子中断源)来的时候,它会把这个中断记录在SRCPND里面:它还要 ...

  4. Session.Abandon-Session.Clear-Session.RemoveAll

    System.Web.UI.Page.Session属性和System.Web.HttpContext.Session属性 都是System.Web.SessionState.HttpSessionS ...

  5. Web Pages version 2兼容 Web Pages version 1的设置

    If you want to run a site using Web Pages version 1 (instead of the default, as in the previous poin ...

  6. URAL 1204. Idempotents (扩展欧几里得)

    题目链接 题意 : 给你一个同余方程, x*x ≡ x  (mod n),让你求出所有的小于n的x. 思路 : 先来看同余的概念 :给定一个正整数m,如果两个整数a和b满足a-b能被m整除,即m|(a ...

  7. Position Independent Code (PIC) in shared libraries

    E原文地址:http://eli.thegreenplace.net/2011/11/03/position-independent-code-pic-in-shared-libraries/下一文: ...

  8. TriggerAction扩展----ExInvokeCommandAction

    Wp&Win8中使用命令绑定时,除了Button控件自带命令绑定,其他的时候是用Interactivity库中的InvokeCommandAction实现的(Win8 需要额外安装第三方NuG ...

  9. cocos学习

    第一章 JavaScript 快速入门 1.1 变量 在 JavaScript 中,我们像这样声明一个变量: var a; 保留字 var 之后紧跟着的,就是一个变量名,接下来我们可以为变量赋值: v ...

  10. rsync 备份服务搭建(完成)

    rsync服务守护进程 服务器端配置过程: 1. 检查rsync是否安装: rpm -qa rsync 2.添加rsync服务的用户,管理本地目录 useradd-s /sbin/nologin -M ...