码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 642 - Word Amalgamation

时间:2014-10-24 13:00:33      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   sp   2014   on   log   amp   

题目:给你一个单词列表,再给你一些新的单词,输出列表中重新排列能得到此新单词的词。

分析:字符串。对每个字符串的字母排序生成新的传f(str),整体排序,用二分来查找即可。

说明:注意输出要满足字典序,先排序后查找。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

typedef struct wnode
{
	char word[7];
	char abcd[7];
}words;
words W[101];

int cmp(words a, words b)
{
	int c = strcmp(a.abcd, b.abcd);
	if (c != 0) return c<0;
	return strcmp(a.word, b.word)<0;
}

int bs(char str[], int r)
{
	int l = 0,m,c;
	while (l < r) {
		m = (l+r)/2;
		c = strcmp(W[m].abcd, str);
		if (c < 0) 
			l = m+1;
		else r = m;
	}
	return l;
}

char buf[7];

int main()
{
	int count = 0;
	while (gets(W[count].word) && strcmp(W[count].word, "XXXXXX")) {
		strcpy(W[count].abcd, W[count].word);
		sort(W[count].abcd, W[count].abcd+strlen(W[count].abcd));
		count ++;
	}
	
	sort(W, W+count, cmp);
	
	while (gets(buf) && strcmp(buf, "XXXXXX")) {
		sort(buf, buf+strlen(buf));
		int s = bs(buf, count-1),flag = 0;
		while (!strcmp(buf, W[s].abcd)) {
			printf("%s\n",W[s ++].word);
			flag = 1;
		}
		if (!flag) 
			printf("NOT A VALID WORD\n");
		printf("******\n");
	}
	return 0;
}

UVa 642 - Word Amalgamation

标签:blog   io   os   ar   sp   2014   on   log   amp   

原文地址:http://blog.csdn.net/mobius_strip/article/details/40425669

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!