【ybtoj 高效进阶 2.1】【字符串】 字符串环

【ybtoj 高效进阶 2.1】【字符串】 字符串环

题目

在这里插入图片描述


解题思路

将字符串累加本身
从中截取一段
查找另一字符串是否包含
输出最大长度


代码

#include <iostream>
#include <cstdio>
#include <cstdio>
using namespace std;
string x, y;
int lx, ly, ans;
int main() {
    
    
    cin >> x >> y;
    x = x + x;
    y = y + y;
    lx = x.size();
    ly = y.size();
    for (int i = 0; i < lx; i++)
        for (int j = 1; j < min(lx - i + 1, ly); j++) {
    
    
            string s = x.substr(i, j);
            if (y.find(s) != -1)
                ans = max(ans, j);
        }
    cout << ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45621109/article/details/113358187