1 条题解
-
0
C++ :
#include <iostream> #include <string> #include <cstdio> using namespace std; bool isSubStr(const std::string& str, int index1, int index2, int len) { for (int i(0); i != len; ++i) { if (str[index1 + i] != str[index2 + i]) return false; } return true; } // 在串中找以index开始,长度为len的串的数量 int repeat(const std::string& str, int index, int len) { int rep(1); for (unsigned i(index + 1); i <= str.size() - len; ++i) { if (isSubStr(str, index, i, len)) ++rep; } return rep; } int main() { //freopen("1.txt","r",stdin); //freopen("2.txt","w",stdout); int L; string S; int M; cin >> M; while(M--) { cin >> L >> S; int maxRep(0), maxIndex(0), maxLen(0); // 子串长度 for (int len(S.size() - 1); len != L-1; --len) { for (unsigned i(0); i != S.size() - len; ++i) { // 从当前下标开始, len长度的子串 int rep = repeat(S, i, len); if (maxRep < rep) { maxRep = rep; maxIndex = i; maxLen = len; } } } cout << S.substr(maxIndex, maxLen); cout << endl; } return 0; }
- 1
信息
- ID
- 2883
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者