字符串c++字符环

     今天这个题目及其难。还好靠身边的丁冠宇大佬帮忙,最后还是把这道题给搞出来了!!!

这里给大家分享一下。希望能帮助到各位。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
    int x, y, len, len1, len2, limit, ans=0;
    string s1, s2;
    cin >> s1 >> s2;
    len1 = s1.size();
    len2 = s2.size();
    limit = min(len1, len2);
    s1 += s1; // 造环
    s2 += s2; // 造环
    for (int i=0; i<len1; i++){
        for (int j=0; j<len2; j++){
            len = 0;
            x = i;
            y = j;
            while (s1[x++]==s2[y++] && len<limit) len++;
            if (len > ans) ans = len;
        }
    }
    cout << ans << endl;
    return 0;
}

  

原文地址:https://www.cnblogs.com/5t2y0/p/9240582.html