POJ 1002 487-3279(Map)
2014-05-12 21:01
375 查看
可以用Map来求解,很重要的一点就是C++的Map会按照key的字典序来自动排列数据。
这样就大大简化了,至少少了排序的一步。
一开始我用的是Map<string, int> ,不得不说转换成数字存储的时候是比较简单,但是也慢多了。显然就出现了TLE。
然后换,换成 map<int, int> 的,通过乘,来累计得到数字。
但是还是WA。
原来是0000000的问题。
用printf 限制一下,这样才能通过。
最后AC Memory : 3976K Time : 829MS
代码:
这样就大大简化了,至少少了排序的一步。
一开始我用的是Map<string, int> ,不得不说转换成数字存储的时候是比较简单,但是也慢多了。显然就出现了TLE。
然后换,换成 map<int, int> 的,通过乘,来累计得到数字。
但是还是WA。
原来是0000000的问题。
用printf 限制一下,这样才能通过。
最后AC Memory : 3976K Time : 829MS
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <map> using namespace std; map<int,int> num; map<int,int>::iterator iter; char s[4000]; char tem[4000]; int st(char ch) { if(ch=='A' || ch=='B' || ch=='C') return 2; if(ch=='D' || ch=='E' || ch=='F') return 3; if(ch=='G' || ch=='H' || ch=='I') return 4; if(ch=='J' || ch=='K' || ch=='L') return 5; if(ch=='M' || ch=='N' || ch=='O') return 6; if(ch=='P' || ch=='R' || ch=='S') return 7; if(ch=='T' || ch=='U' || ch=='V') return 8; if(ch=='W' || ch=='X' || ch=='Y') return 9; } int main() { int N; scanf("%d",&N); for(int i = 0; i<N; ++i) { scanf("%s",tem); int x = 0; for(int j=0; tem[j]!='\0'; ++j) { if(tem[j]=='-' || tem[j]=='Q' || tem[j]=='Z') continue; else if(tem[j]<='9') x=x*10+tem[j]-'0'; else if(tem[j]<='Z') x=x*10+st(tem[j]); } ++num[x]; } bool have = false; for(iter = num.begin(); iter!=num.end(); iter++) { if(iter->second > 1) { have = true; int k1 = (int)iter->first / 10000; int k2 = (int)iter->first %10000; printf("%03d-%04d",k1,k2); printf(" %d\n",iter->second); } } if(!have) cout<<"No duplicates."<<endl; return 0; }
代码:
相关文章推荐
- POJ 1002 487-3279(map应用)
- POJ 1002 487-3279(map)
- poj 1002 487-3279之map解法
- poj 1002(487-3279)STL中的map
- POJ-1002 487-3279(map)
- POJ1002 487-3279(map映射)
- POJ 1002 487-3279 MAP||Sort
- poj 1002 487-3279(使用STL map)
- poj 1002 487-3279(map)
- POJ 1002 487-3279(map映照容器的使用)
- poj 1002 487-3279 map的使用
- 【String+Map版】从poj 1002——487-3279的STL做法和若干陷阱来分析MAP
- POJ 1002 487-3279
- POJ 1002 487-3279 Trie题解
- uva 755/poj 1002 487-3279
- poj1002 -- 487-3279
- POJ1002 487-3279
- POJ1002 487-3279
- POJ 1002 - 487-3279 STL
- poj 1002 487-3279(参考YSQ)