#include<bits/stdc++.h> using namespace std; int main(){ string s; while(cin >> s){ int ls = s.length(); int max1 = 0; for(int i = 0 ; i < ls ; i++){ for( int j = i + 1 ; j < ls ; j++){ int sum = 0; while(s[i + sum] == s[j + sum] ){ sum++; } max1 = max(sum,max1); } } cout << max1 << endl; } return 0; }
算法步骤:每排完一次去一个字符,即每一个字符都往后找有没有能够匹配的字符串