POJ 1002 UVA 755 487--3279 电话排序 简单但不容易的水题

题意:给你许多串字符串,从中提取电话号码,输出出现复数次的电话号码及次数。

以下是我艰难的AC历程:(这题估计是我刷的题目题解次数排前的了。。。)

题目不是很难理解,刚开始想到用map,但stl的map不是很放心,怕超时。于是放心的用数组敲了。(事实证明我放心过头了)

刚开始在Vjugde里面提交老SE,我还以为uva又挂了,最近各种挂啊。。。

后来又刷了一题过来提交,还是SE。某大神说这题uva完全挂了,没人能提交得了,只能去poj交,于是屁颠屁颠跑poj给TLE了。

在敲的时候就考虑到用getchar一个一个读字符再判断会很耗时,果然超时了。于是改成gets,然后一个一个字符的判断。

本来感觉用数字储存电话号码输出时不是很好弄,于是用字符串,但是sort时字符串会慢很多,如今TLE和这个也有关系吧。。。于是改成int数组储存。

然后就是无尽的WA了。。。

注意到这句,"If there are no duplicates in the input print the line:No duplicates."改了下还是WA。

找了一会发现排序部分有些bug,到最末尾的电话号码如果是复数的话不会输出。于是在它后面多了组检查输出。

继续wa我就郁闷了。逛别人的博客发现大家都卡这题,据说数据很BT。

找了好久,尝试把存字符串的数组开大,竟然AC了。。特无语。原来开的是20,后来测试发现开到30就可以了。。

竟然卡在这种地方,真是血淋淋的教训。


总结

1.一定要用最优的方法考虑题目,不要偷懒,一直把数据当作最BT的!

2.读入效率:getchar < cin < scanf < gets

3.在某博客发现用scanf("%d/n", &num);能把回车读掉了。

4.操作速度:位运算 < 整数 < 字符串。


代码:(1092K 422MS)

 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

const int maxn = 100100;

char str[300];
int s[maxn] = {0};

int main() {
	str['A'] = str['B'] = str['C'] = 2;
	str['D'] = str['E'] = str['F'] = 3;
	str['G'] = str['H'] = str['I'] = 4;
	str['J'] = str['K'] = str['L'] = 5;
	str['M'] = str['N'] = str['O'] = 6;
	str['P'] = str['R'] = str['S'] = 7;
	str['T'] = str['U'] = str['V'] = 8;
	str['W'] = str['X'] = str['Y'] = 9;
	int t;
	scanf("%d\n", &t);
	char tmp[50];
	for (int i = 0; i < t; i++) {
		gets(tmp);
		for (int j = 0; tmp[j] != '\0'; j++) {
			if (isdigit(tmp[j]))
				s[i] = s[i] * 10 + tmp[j] - '0';
			else if (isalpha(tmp[j]))
				s[i] = s[i] * 10 + str[tmp[j]];
		}
	}
	sort (s, s + t);
	int cnt = 1, flag = true;
	for (int i = 1; i < t; i++)
		if (s[i] != s[i - 1]) {
			if (cnt > 1) {
				printf("%03d-%04d %d\n", s[i - 1] / 10000, s[i - 1] % 10000, cnt);
				flag = false;
			}
			cnt = 1;
		}
		else
			cnt++;
	if (cnt > 1) {
		printf("%03d-%04d %d\n", s[t - 1] / 10000, s[t - 1] % 10000, cnt);
		flag = false;
	}
	if (flag)
		printf("No duplicates.\n");
	return 0;
}


优化了一下,重新修改提交了N次。内存和时间减少到788K360MS。

 

先把那一大块数组去掉改成小巧的整数数组。

在优化时发现一个非常坑爹的事情:在第4行后面多加个空行就会多了15MS。。。

优化代码

 

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int s[100001] = {0}, l[25] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,0,7,7,8,8,8,9,9,9}, cnt, flag, t;
char tmp[30];

int main() {
	scanf("%d\n", &t);
	for (int i = 0; i < t; i++) {
		gets(tmp);
		for (int j = 0; tmp[j] != '\0'; j++) {
			if (tmp[j] >= '0' && tmp[j] <= '9')
				s[i] = s[i] * 10 + tmp[j] - '0';
			else if (tmp[j] >= 'A' && tmp[j] <= 'Z')
				s[i] = s[i] * 10 + l[tmp[j] - 'A'];
		}
	}
	sort (s, s + t);
	cnt = flag = 1;
	for (int i = 1; i < t; i++)
		if (s[i] != s[i - 1]) {
			if (cnt > 1) {
				printf("%03d-%04d %d\n", s[i - 1] / 10000, s[i - 1] % 10000, cnt);
				if (flag == true) flag = false;
			}
			cnt = 1;
		}
		else
			cnt++;
	if (cnt > 1) {
		printf("%03d-%04d %d\n", s[t - 1] / 10000, s[t - 1] % 10000, cnt);
		if (flag == true) flag = false;
	}
	if (flag)
		printf("No duplicates.\n");
	return 0;
}


 



 

posted @ 2013-07-31 19:32  坚固66  阅读(212)  评论(0编辑  收藏  举报