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. pandas dataframe 满足条件的样本提取

    pandas 的dataframe 对 数据查询可以通过3种方式 . 预备知识: 1. pandas 的索引和label都是从0开始的计数的 2. 时间切片都是左闭右开的. [5:6,:]  只会输出 ...

  2. poj 1611 The Suspects(第一道并查集)

    题意: 有N个学生,编号为0-n-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染, 并且这些人如果还参加了别的社团,他所在的社团照样全部感染,社团个数为m,求感染的人数. 输入: n代表人 ...

  3. MySQL性能调优与架构设计——第3章 MySQL存储引擎简介

    第3章 MySQL存储引擎简介 3.1 MySQL 存储引擎概述 MyISAM存储引擎是MySQL默认的存储引擎,也是目前MySQL使用最为广泛的存储引擎之一.他的前身就是我们在MySQL发展历程中所 ...

  4. python3 tkinter 获取输入字符串长度

    python 3  获取输入字符长度 #-*- coding:utf-8 -*- from tkinter import * from tkinter import messagebox def ge ...

  5. 使用Jenkins远程部署war包到tomcat container

    Jenkins首先使用maven将源代码进行编译打包,之后需要将war包传送到tomcat服务器上进行部署. 来看一下Jenkins的基本配置,首先需要安装插件"Deploy to cont ...

  6. DOM--sql server

    public List<LianHeData> select(int ID) { List<LianHeData> list = new List<LianHeData& ...

  7. Java Script 脚本的几种基本格式:

    1. <script>       document.Write("Hello wrrld!!!");     </script> 2. <scrip ...

  8. Android app如何加密?

    欢迎访问网易云社区,了解更多网易技术产品运营经验. Android App包含的内容有dex文件,so文件,res,assets资源文件.对应的加密按此内容分为三大方面:dex保护.so加密.资源保护 ...

  9. 【01】循序渐进学 docker:到底是啥

    写在前面的话 首先说一下,我本身是做运维的,4 年工作,多家公司.所以可能接下来谈到的更多的是一些在工作过程中积累的个人看法.且有些并不具备普遍性,有不合适的地方,全当我在吹牛逼就行. 一开始我们得谈 ...

  10. pdo 预处理

      <?php /* * pdo 预处理sql */ $dsn = "mysql:dbname=0328;host=localhost"; $username = " ...