搜索 HOJ 1193 Anagram checker
2014-02-06 11:38
316 查看
Anagram checker
My Tags | (Edit) |
---|
Source : UVA - V1 | |||
Time limit : 1 sec | Memory limit : 32 M |
9
It is often fun to see if rearranging the letters of a name gives an amusing anagram. For example, the letters of `WILLIAM SHAKESPEARE' rearrange to form `SPEAK REALISM AWHILE'.
Write a program that will read in a dictionary and a list of phrases and determine which words from the dictionary, if any, form anagrams of the given phrases. Your program must find all sets of words in the dictionary which can be formed from the letters in
each phrase. Do not include the set consisting of the original words. If no anagram is present, do not write anything, not even a blank line.
Input
The dictionary will be in alphabetic order and will contain up to 2000 words, one word per line. The entire file will be in upper case, and no dictionary word or phrase will contain more than 20 letters. You cannot assume the language being used is English.
Output
appear in alphabetic sequence.
Sample
input
ABC AND DEF DXZ K KX LJSRT LT PT PTYYWQ Y YWJSRQ ZD ZZXY # ZZXY ABC DEF SXZYTWQP KLJ YRTD ZZXY YWJSRQ PTYYWQ ZZXY #
Sample
output
SXZYTWQP KLJ YRTD = DXZ K LJSRT PTYYWQ SXZYTWQP KLJ YRTD = DXZ K LT PT Y YWJSRQ SXZYTWQP KLJ YRTD = KX LJSRT PTYYWQ ZD SXZYTWQP KLJ YRTD = KX LT PT Y YWJSRQ ZD
题意:给出一个字符串重新组合后,问能不能用给出字典里面的单词表示。
思路:直接记录给个字符串每个字符的个数,然后匹配的时候只要单词的字母不会超过字符串中对应字母的个数,就能选择这个单词。然后搜索一次就行了
代码:
#include<algorithm> #include<iostream> #include<cstring> #include<string.h> #include<string> #include<vector> #include<cassert> using namespace std; #define eps 1e-8 #define LL long long int cnt[30] , n; char dict[2010][30] , in[1000] , sort_in[1000] ; int cost[2010][30]; int ans[2010] , top; bool vis[2010]; int cmp(const void * s1 , const void * s2) { return strcmp((char*)s1,(char*)s2); } void sort_input() { char buffer[2010][200]; char in_tmp[1000]; strcpy(in_tmp,in); char * p = strtok(in_tmp," "); int sz = 0; while (p) { strcpy(buffer[sz],p); ++sz; p = strtok(NULL," "); } qsort(buffer,sz,sizeof(buffer[0]),cmp); sort_in[0] = 0; for (int i = 0 ; i < sz ; ++i) { strcat(sort_in," "); strcat(sort_in,buffer[i]); } } void init() { memset(cost,0,sizeof(cost)); top = 0; } void input() { while (scanf("%s",dict )) { if (dict [0]=='#') { gets(dict ); break; } for (int i = 0 ; i < strlen(dict ) ; ++i) ++cost [dict [i]-'A']; ++n; } } bool canput(int j) { for (int i = 0 ; i < 26 ; ++i) if (cnt[i] < cost[j][i]) return false; return true; } void dfs(int rest,int cur) { if (rest==0) { char buffer[1000]; buffer[0] = 0; for (int i = 0 ; i < top ; ++i) { strcat(buffer," "); strcat(buffer,dict[ans[i]]); } sort_input(); if (strcmp(sort_in,buffer)) printf("%s =%s\n",in,buffer); return; } for (int i = cur ; i < n ; ++i) if (!vis[i] && canput(i)) { for (int j = 0 ; j < 26 ; ++j) { cnt[j] -= cost[i][j]; rest -= cost[i][j]; } ans[top++] = i; vis[i] = true; dfs(rest,i+1); vis[i] = false; --top; for (int j = 0 ; j < 26 ; ++j) { cnt[j] += cost[i][j]; rest += cost[i][j]; } } } void solve() { while (gets(in)) { if (in[0]=='#') return; memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis)); int sum = 0; for (int i = 0 ; i < strlen(in) ; ++i) if (isalpha(in[i])) ++cnt[in[i]-'A'] , ++sum; dfs(sum,0); } } int main() { n = 0; while (scanf("%s",dict )==1) { init(); for (int i = 0 ; i < strlen(dict ) ; ++i) ++cost [dict [i]-'A']; ++n; input(); solve(); n = 0; } }
相关文章推荐
- poj 2197 hoj 2027 Jill's Tour Paths 搜索
- 搜索 HOJ 1749 Doggone Moles
- 搜索 HOJ 1126 Smith Numbers
- (简单)搜索 HOJ 1075 The Castle
- 【搜索剪枝】HOJ 1239 Calling Extraterrestrial Intelligence Again
- 搜索 HOJ 1736 A Game on the Chessboard
- (简单) 搜索 HOJ 1085 Finding Rectangles
- poj1111 hoj 1639 Image Perimeters 简单搜索
- 搜索 HOJ 1692 Choose Your Own Adventure
- (简单) 搜索 HOJ 1097 Robot
- BZOJ 1193 搜索+贪心
- 搜索 HOJ 1629 Safecracker
- (简单) 搜索 HOJ 1105 Egyptian Multiplication
- BZOJ 1193 搜索+贪心
- 搜索 HOJ 1619 The Die Is Cast
- (简单) 搜索 最短路 HOJ 1121 Erdos Numbers
- 搜索 HOJ 1114 Frame Stacking
- 搜索 HOJ 1114 Frame Stacking
- 搜索 HOJ 1394 Shortcut
- Go hdu 4158 hoj 简单搜索