洛谷题号:P2724
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include<iostream> #include<algorithm> #include<cstring> #include<vector> #include<string> #include<cassert> #include<map> using namespace std; map<string, int> m; string s,st; int a, b, n; struct sorted{ int times; string str; bool operator < (sorted & s2){ if (times != s2.times)return times>s2.times; else if (str.length() != s2.str.length())return str.length() < s2.str.length(); else{ for (int i = 0; i < str.length(); i++){ if (str[i] != s2.str[i]){ return str[i] < s2.str[i]; } } return true; } } }sarr[200000]; int sarr_iter = 0; int main(){ ios::sync_with_stdio(false); cin >> a >> b >> n; while (cin >> st)s.append(st); for (int len = a; len <= b; len++){ if (len > s.length())break; for (int i = 0; i <= s.length() - len; i++){ m[s.substr(i, len)]++; } } for (auto i = m.begin(); i != m.end(); i++){ sarr[++sarr_iter].times = (*i).second; sarr[sarr_iter].str = (*i).first; } sort(sarr + 1, sarr + sarr_iter + 1); int cur_freq_num = 0; int cur_freq_value = -1, cur_value_times = 0; for (int i = 1; cur_freq_num<=n&&i<=sarr_iter; i++){ if (sarr[i].times != cur_freq_value){ cur_freq_num++; if (cur_freq_num > n)break; if (cur_value_times % 6 != 0)cout << endl; cur_freq_value = sarr[i].times; cur_value_times = 1; cout << cur_freq_value << endl << sarr[i].str; } else{ if (cur_value_times % 6 != 0)cout << " "; cout << sarr[i].str; cur_value_times++; if (cur_value_times % 6 == 0)cout << endl; } } if (cur_value_times % 6 != 0)cout << endl; } |