题目:最大值
来源:“科林明伦杯”哈尔滨理工大学第十届程序设计竞赛(同步赛)
解题思路
题目:给定字符串 s,求与字符串的前缀相同的非前缀子串的最大长度。
使用双指针,分别指向字符串的前缀和非前缀子串。
C++代码
#include<iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t){
string s;
cin >> s;
int cnt = 0;
int i = 0;
for(int j=1; j<s.length(); ++j){
if(s[i] == s[j]){
cnt = max(cnt, i+1);
++i;
}
else{
i = 0;
}
}
cout << cnt << endl;
--t;
}
return 0;
}
京公网安备 11010502036488号