这题类似于1787,最后的节点一定是两点的LCA,这里也就是两个字符串的最长公共前缀。
# include <cstdio> # include <cstring> # include <cstdlib> # include <iostream> # include <vector> # include <queue> # include <stack> # include <map> # include <set> # include <cmath> # include <algorithm> using namespace std; # define lowbit(x) ((x)&(-x)) # define pi 3.1415926535 # define eps 1e-9 # define MOD 100000007 # define INF 1000000000 # define mem(a,b) memset(a,b,sizeof(a)) # define FOR(i,a,n) for(int i=a; i<=n; ++i) # define FO(i,a,n) for(int i=a; i<n; ++i) # define bug puts("H"); # define lch p<<1,l,mid # define rch p<<1|1,mid+1,r # define mp make_pair # define pb push_back typedef pair<int,int> PII; typedef vector<int> VI; # pragma comment(linker, "/STACK:1024000000,1024000000") typedef long long LL; int Scan() { int res=0, flag=0; char ch; if((ch=getchar())=='-') flag=1; else if(ch>='0'&&ch<='9') res=ch-'0'; while((ch=getchar())>='0'&&ch<='9') res=res*10+(ch-'0'); return flag?-res:res; } void Out(int a) { if(a<0) {putchar('-'); a=-a;} if(a>=10) Out(a/10); putchar(a%10+'0'); } const int N=505; //Code begin... char s1[N], s2[N], s3[N]; int sol(char* a, char* b, int alen, int blen){ FO(i,0,min(alen,blen)) if (a[i]!=b[i]) return i; return min(alen,blen); } int main () { int len1, len2, len3, ans=INF, tmp, x, y; scanf("%d%s%d%s%d%s",&len1,s1,&len2,s2,&len3,s3); x=sol(s1,s2,len1,len2); tmp=len1+len2-2*x; y=sol(s1,s3,x,len3); tmp+=(len3+x-2*y); ans=min(ans,tmp); x=sol(s1,s3,len1,len3); tmp=len1+len3-2*x; y=sol(s1,s2,x,len2); tmp+=(len2+x-2*y); ans=min(ans,tmp); x=sol(s2,s3,len2,len3); tmp=len2+len3-2*x; y=sol(s1,s2,x,len1); tmp+=(len1+x-2*y); ans=min(ans,tmp); printf("%d ",ans); return 0; }