题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1830
直接枚举目标串是什么,目标串一定是三个字符串的某一个前缀(注意可能为空),然后判断一下取个最小值。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 55
#define llg long long
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,tot,ans,len,l[maxn]; char s[][maxn],aim[maxn]; llg work(llg x)
{
llg wz=;
for (llg i=;i<=len;i++)
if (aim[i]==s[x][i])
{
wz=i;
}
else break;
if (wz==len) return l[x]-len;
return (l[x]-wz)+len-wz;
} int main()
{
yyj("bzoj1830");
for (llg i=;i<=;i++)
{
cin>>l[i];
cin>>(s[i]+);
}
ans=0x7fffffff;
for (llg k=;k<=;k++)
for (llg i=;i<=;i++)
for (llg j=i-;j<=l[k];j++)
{
for (llg w=;w<maxn;w++) aim[w]='\0';
tot=;
len=j-i+;
for (llg w=;w<=len;w++) aim[w]=s[k][i+w-];
for (llg w=;w<=;w++) tot+=work(w);
ans=min(ans,tot);
}
cout<<ans;
return ;
}