算法简单,写起来遇到些小问题
/* 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++) { ; 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); ; ; ; while (step <= N && cur < res.size()) { //cout << endl << "step: " << step << endl; || res[cur].frequency != res[cur-].frequency) { pre = cur; if (step == N) break; if (cur) fout << endl; fout << res[cur].frequency << endl; fout << res[cur].content; step++; } else { == ) fout << endl << res[cur].content; else fout << " " << res[cur].content; } cur++; } fout << endl; ; }