2872的解法-个人见解

  原题的链接:http://poj.org/problem?id=2872

  咋一看感觉不难,但是还是贡献了四个TLE,比较悲剧。本人水平极有限,只能给还没有ac的同学一点建议。

  关于本题,没有给出数据的规模,所以肯定不小,但是开始我没有想到会那么大。解题的关键就是查找这个地方该如何做,我先试了一下,用vector,果断超时,set也试了一下,发现没有过,后面才发现代码也有问题,囧。过了以后再用set试了一下,TLE,可能是管理员把数据加强了,因为看到有用set过的,或者我代码还有问题。在用set试的时候再贡献了3个ce,就当攒RP吧。个人觉得set应该够快才对,因为set内部就是红黑树,速度是非常快的。我最后的还是用快排加二分过的,写的比较困难。但还是侥幸对了。另外也没什么难的了。贴一下代码,8个sub啊。。

#include<iostream>
#include<fstream>
using namespace std;
const int MAX = 50000;
char s[MAX][200];
//字符串的自定义比较函数,可以写好加到模板里
int cmp(const void *a, const void *b)
{
	return strcmp((char *)a, (char *)b);
}
//为字符串写的二分,就是把数组改改
int search(int l, int h, char *v)
{
	int m;
	while (l < h)
	{
		m = (l + h) >> 1;
		if (!strcmp(s[m], v))return m;
		if (strcmp(s[m], v) < 0)l = m + 1;
		else h = m;
	}
	return -1;
}
int main()
{
	int tot, n, k = 0;
	char a[200];
	scanf("%d", &tot);
	for (int i = 0; i < tot; i++)
		scanf("%s", s[i]);
	qsort(s, tot, sizeof(s[0]), cmp);
	scanf("%d", &n);
	while (n--)
	{
		bool flag = true;
		while (cin>>a && strcmp(a, "-1"))
		{
			int tem = 0;
			if (search(0, tot, a) == -1)
			{
				if (flag)
				{
					flag = false;
					printf("Email %d is not spelled correctly.\n", ++k);
				}
				cout<<a<<endl;
			}
		}
		if (flag)printf("Email %d is spelled correctly. \n", ++k);
	}
	printf("End of Output\n");//不要忘了这个
	return 0;
}
posted @ 2011-02-16 20:17  like@neu  阅读(236)  评论(0编辑  收藏  举报