poj1318 Word Amalgamation 字符串排序(qsort)
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)的更多相关文章
- ZOJ1181 Word Amalgamation 字符串 排序查找
传送门:ZOJ1181 思路:自身排序来判断两个字符串拥有相同的字符. #include<cstdio> #include<cstdlib> #include<io ...
- Word Amalgamation(枚举 + 排序)
Word Amalgamation Time Limit: 1 Sec Memory Limit: 64 MB Submit: 373 Solved: 247 Description In mil ...
- hdu-----(1113)Word Amalgamation(字符串排序)
Word Amalgamation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Uva 642 - Word Amalgamation sort qsort
Word Amalgamation In millions of newspapers across the United States there is a word game called J ...
- hdu1113 Word Amalgamation(详解--map和string的运用)
版权声明:本文为博主原创文章.未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/35338617 转载请注明出 ...
- python实验二:字符串排序
##统计word中的各个字符的出现的次数,并统计出所有前十名的字符使用次数 # -*- coding:utf-8 -*- word='''awfesdafhjkcasadckjsdackjsadvcn ...
- Trie树|字典树(字符串排序)
有时,我们会碰到对字符串的排序,若采用一些经典的排序算法,则时间复杂度一般为O(n*lgn),但若采用Trie树,则时间复杂度仅为O(n). Trie树又名字典树,从字面意思即可理解,这种树的结构像英 ...
- HDOJ.1113 Word Amalgamation(map)
Word Amalgamation 点我挑战题目 点我一起学习STL-MAP 题意分析 给出字典.之后给出一系列======乱序======单词,要求你查字典,如过这个乱序单词对用有多个有序单词可以输 ...
- hdu - 1113 Word Amalgamation (stl)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 给定一个字典,然后每次输入一个字符串问字典中是否有单词与给定的字符串的所有字母一样(顺序可以打乱),按字典 ...
随机推荐
- 28. LAST() 函数
LAST() 函数 LAST() 函数返回指定的字段中最后一个记录的值. 提示:可使用 ORDER BY 语句对记录进行排序. SQL LAST() 语法 SELECT LAST(column_nam ...
- 关于Rest Framework中View、APIView与GenericAPIView的对比分析
关于Rest Framework中View.APIView与GenericAPIView的对比分析 https://blog.csdn.net/odyssues_lee/article/detail ...
- dev初识 拖动分组
1.前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm ...
- SQL Server 时间类型转换函数
cast ( expression as data_type(length))convert ( data_type (length), expression, style) //如果未指定 leng ...
- struts2 、mybatis 、easyui 分页
rows page 控件自动提交这两个参数 pageSize number The page size. 10pageNumber number Show the page number when p ...
- URLRewrite 实现方法详解
所谓的伪静态页面,就是指的URL重写,在ASP.NET中实现非常简单首先你要在你的项目里引用两个DLL:ActionlessForm.dll.URLRewriter.dll,真正实现重写的是 URLR ...
- orcl 对table的一些操作
删除 table:drop table 表名: 恢复删除 : flashback table 表名 to before drop: 清空table : truncate table 表名; 恢复清空: ...
- angular HTML属性绑定
- Task 回调
前正无生意,且记录Task回调之用法. using System; using System.Collections.Generic; using System.Diagnostics; using ...
- Java读写配置文件prop.properties
Java读写配置文件prop.properties @Test public void fun() throws IOException{ Properties prop=new Properties ...