搜索 HOJ 1193 Anagram checker

Anagram checker

My Tags   (Edit)
  Source : UVA - V1
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 18, Accepted : 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

Input will consist of two parts. The first part is the dictionary, the second part is the set of phrases for which you need to find anagrams. Each part of the file will be terminated by a line consisting of a single #. 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

Output will consist of a series of lines. Each line will consist of the original phrase, a space, an equal sign (=), another space, and the list of words that together make up an anagram of the original phrase, separated by exactly one space. These words must 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[n]))
	{
		if (dict[n][0]=='#')
		{
			gets(dict[n]);
			break;
		}
		for (int i = 0 ; i < strlen(dict[n]) ; ++i)
			++cost[n][dict[n][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[n])==1)
	{
		init();
		for (int i = 0 ; i < strlen(dict[n]) ; ++i)
			++cost[n][dict[n][i]-'A'];
		++n;
		input();
		solve();
		n = 0;
	}
}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值