问题描述
输入
字符串A
字符串B
输出
一个整数表示答案
样例输入
aaaba
aabba
样例输出
3
算法讨论
暴力可过。
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
#define maxn 100000
char a[maxn],b[maxn],t[maxn];
int n,m,l,maxx;
bool check()
{
int i=1,j=l;
while (i<=j)
if (t[i]!=t[j])
return 0;
else
i++,j--;
i=1; j=0;
while (i<=l)
{
while (t[i]!=a[j] && j<=n)
j++;
if (j>n && i<=l)
return 0;
i++; j++;
}
return 1;
}
int dfs(int dep,int p)
{
if (dep>m)
return 0;
for (int i=p;i<=m;i++)
{
t[++l]=b[i];
if (check() && l>maxx)
maxx=l;
dfs(dep+1,i+1);
t[l--]=' ';
}
}
int main()
{
gets(a);
gets(b);
n=strlen(a)-1;
m=strlen(b)-1;
dfs(0,0);
printf("%d",maxx);
}
Pixiv ID:63593225