#include #include #include using namespace std; pair fun(const string &str) { vector substrs; int len = str.length(); string substring; int maxcount(0); //后缀数组 for (int i = 0; i < len; ++i) { substrs.push_back(str.substr(i)); // cout << substrs[i] << endl; } for (int i = 0; i < len; ++i) { for (int j = 1; j <= len; j++) { int count = 1; int sublen = j; for (int k = i + 1; k < len; k++) { if (substrs[k].length() < sublen) { break; } string str1 = substrs[i].substr(0, sublen); string str2 = substrs[k].substr(0, sublen); if (str1.compare(str2)==0) { ++count; } } if (count > maxcount||(count == maxcount && sublen > substring.length())) { maxcount = count; substring = substrs[i].substr(0, sublen); } } } return make_pair(maxcount, substring); } int main() { string str; int len; cin >> len; cin >> str; auto res = fun(str); cout << res.first << endl; // cout << "the matched substring is:" << res.second << endl; return 0; }