hdoj 1113 Word Amalgamation 【判断一个字符串能否被一个字符串集里的字符串经过变换而得到】
来源:互联网 发布:穆逢春 知乎 编辑:程序博客网 时间:2024/05/21 18:30
Word Amalgamation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2810 Accepted Submission(s): 1351
Problem 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.
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
tarpgivenscorerefundonlytrapworkearncoursepepperpartXXXXXXresconfudreaptrsettoresucXXXXXX
Sample Output
score******refund******parttarptrap******NOT A VALID WORD******course******
题意:给出一个字符串集,以XXXXXX结束。再给出一系列目标字符串,也以XXXXXX结束。判断目标字符串能否被字符串集里的字符串经过变换而得到,若不能输出
NOT A VALID WORD,反之 按字典序输出那些可以变换得到目标字符串的字符串,每个目标字符串测试结束输入******。
思路:按照字典序变换并记录 字符串集里面的每个字符串,对于目标字符串 也按字典序排列,并找出 与 排序后的目标字符串 相等的串存储其变换前对应字符串,按字典序排序后输出即可。
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct rec{char st[10];}num[110];char word[110][10];//输入的单词 char convert[110][10];//变换后的单词 char str[10];//查询的单词int vis[110];//标记已经用过的单词 bool cmp(rec a, rec b)//按照字典序排列 字符串 {return strcmp(a.st, b.st) < 0;} int main(){int n = 0;int i;int t, exist;//记录找到的字符串数目 和 当前字符串是否能找到匹配串 while(scanf("%s", word[n]), strcmp(word[n], "XXXXXX")){vis[n] = 0; strcpy(convert[n], word[n]);sort(convert[n], convert[n] + strlen(convert[n]));n++;}while(scanf("%s", str), strcmp(str, "XXXXXX")){t = 0;//记录匹配的字符串数目 exist = 0;//标记是否找到 for(i = 0; i < n; i++){if(vis[i]) continue;sort(str, str+strlen(str));if(strcmp(str, convert[i]) == 0){exist = 1;strcpy(num[t++].st ,word[i]);}}if(!exist)printf("NOT A VALID WORD\n");else{sort(num, num+t, cmp);for(i = 0; i < t; i++)printf("%s\n", num[i].st);}printf("******\n");} return 0;}
0 0
- hdoj 1113 Word Amalgamation 【判断一个字符串能否被一个字符串集里的字符串经过变换而得到】
- ACM--字符串--HDOJ 1113--Word Amalgamation--map
- hdoj 1113 Word Amalgamation (字符串处理)
- 判断一个字符串能否成为合法的文件名
- java判断一个字符串在另一个字符串里的个数
- java判断一个字符串在另一个字符串里的个数
- HDU 1113 Word Amalgamation -- 字符串处理+排序
- HDU 1113 Word Amalgamation(字符串&&STL)
- 判断字符串s2能否经过s1循环移位得到(strstr函数)
- 输入一个字符串,判断该字符串插入一个字符,能否使其成为回文字符串
- 判断一个字符串的字符重新排列后,能否变成另一个字符串。
- 判断一个字符串是否在一个字符串数组里
- 判断一个字符串是否由另一个字符串旋转而成
- 判断一个字符串是否是一个字符串的旋转字符串
- 如何判断一个字符串是否经过base64加密过
- 判断一个字符串是否经过了base64_encode加密
- 判断一个字符串能否转化为日期格式
- java如何判断一个字符串能否转化为int
- Audio--- HTML5环形音乐播放器
- 进入房间报错。
- 最好用的SAT数学解题方法
- Android学习记录之------R文件的优化
- maven多环境打包配置
- hdoj 1113 Word Amalgamation 【判断一个字符串能否被一个字符串集里的字符串经过变换而得到】
- selenium
- 简单Clock算法
- Postman - 測試 API 的好工具
- GRE作文备考——教育的三大目的
- STL学习笔记之前篇
- 编译android源码提示/usr/bin/jar命令不存在问题的解决方法
- gcc -rdynamic参数解释
- recommenderjob原码分析