USACO Section 3.1: Contact
2013-07-20 16:55
267 查看
算法简单,写起来遇到些小问题
/* ID: yingzho1 LANG: C++ TASK: contact */ #include <iostream> #include <fstream> #include <string> #include <map> #include <vector> #include <set> #include <algorithm> #include <stdio.h> #include <queue> #include <cstring> #include <cmath> #include <list> using namespace std; ifstream fin("contact.in"); ofstream fout("contact.out"); int A, B, N; string input; struct fre { int frequency; string content; }; //bool cmp(const int &a, const int &b) {return a > b;} bool cmp(const fre &a, const fre &b) { if (a.frequency == b.frequency) { if (a.content.size() == b.content.size()) return a.content < b.content; else return a.content.size() < b.content.size(); } else return a.frequency > b.frequency; } int main() { fin >> A >> B >> N; while (!fin.eof()) { string frag; getline(fin, frag); if (!input.size()) input = frag; else input = input + frag; } map<string, int> rec; for (int len = A; len <= B; len++) { for (int cur = 0; cur + len <= input.size(); cur++) { rec[input.substr(cur, len)]++; } } vector<fre> res; for (map<string, int>::iterator it = rec.begin(); it != rec.end(); it++) { fre tmp; tmp.frequency = it->second; tmp.content = it->first; res.push_back(tmp); } sort(res.begin(), res.end(), cmp); int step = 0; int cur = 0; int pre = 0; while (step <= N && cur < res.size()) { //cout << endl << "step: " << step << endl; if (cur == 0 || res[cur].frequency != res[cur-1].frequency) { pre = cur; if (step == N) break; if (cur) fout << endl; fout << res[cur].frequency << endl; fout << res[cur].content; step++; } else { if ((cur-pre) % 6 == 0) fout << endl << res[cur].content; else fout << " " << res[cur].content; } cur++; } fout << endl; return 0; }
相关文章推荐
- USACO-Section 3.1 Contact(枚举)
- USACO Section 3.1 Contact (contact)
- usaco Chapter 3 section 3.1 Contact
- USACO-Section 3.1-PROB Contact
- USACO-Section3.1 Contact【暴力枚举】
- usaco-Section 3.1-Contact
- USACO section 3.1 Contact(AC自动机)
- USACO section 3.1 Contact(AC自动机)
- USACO-Section3.1 Contact【动态规划(背包问题)】
- USACO 3.1 Contact (contact)
- USACO 3.1 Contact
- 最小生成树基础模板题(USACO Training Section 3.1 最短网络 Agri-Net)
- usaco Chapter 3 section 3.1 Humble Numbers
- usaco 3.1 Contact
- USACO-Section 3.1-PROB Score Inflation
- USACO-Section 3.1 Stamps(DP)
- USACO Section 3.1 Agri-Net (agrinet)
- USACO 3.1 Contact
- USACO Section 3.1 Humble Numbers
- usaco Chapter 3 section 3.1 Shaping Regions