最长公共前后缀
作者:互联网
- 数据小直接暴力
- 遍历字串长度(从大到小),如果满足条件直接输出长度
- substr函数使用
#include <bits/stdc++.h>
using namespace std;
#define MAX 100001
string a, b;
int main()
{
cin >> a;
cin >> b;
if (a.length() < b.length())
{
swap(a, b);
}
for (int lenth = b.length(); lenth >= 0; lenth--)
{
string prea = a.substr(0, lenth), suba = a.substr(a.length() - lenth, lenth);
string preb = b.substr(0, lenth), subb = b.substr(b.length() - lenth, lenth);
if (prea == subb || suba == preb)
{
printf("%d", lenth);
return 0;
}
}
}
标签:int,string,后缀,prea,substr,length,lenth,公共,最长 来源: https://www.cnblogs.com/Wang-Xianyi/p/16536127.html