题目链接:http://livearchive.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2995
—————————————————————————————————————————————
题目描述:
就是把几个字符串收尾拼接,可重合的部分去掉。求最小长度。
注意,只能首尾拼接,ABCDE和CD 应为7.
—————————————————————————————————————————————
题目思路:
用一下next_permutation即可。
拼接的时候,对已经拼好的部分和新添加的部分进行对比,从不重合的地方开始拼。
另:
不裸的做法:http://acm.hust.edu.cn:8080/judge/contest/viewSource.action?id=727585
—————————————————————————————————————————————
题目细节:
1、注意题意,不可安在中间。
2、又忘了把 freopen去掉了 = =
—————————————————————————————————————————————
源代码:
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)>(b)?(b):(a))
#define inf 1000
char s[8][15];
int a[10],n = 0;
int mystrcmp(char *s1,int i,char *s2)
{
int j = 0,k = 0,t = 0;
for(j = 0;j<i;j++)
{
for(k = 0;k<strlen(s2);k++)
{
if(k+j == i)
{
for(t = k;t<strlen(s2);t++)
s1[i++] = s2[t];
return strlen(s2) - k;
}
if(s1[j+k]!=s2[k])
break;
}
if(k == strlen(s2) && k+j == i)
return 0;
}
for(k = 0;k<strlen(s2);k++)
s1[i++] = s2[k];
return strlen(s2);
}
int judge()
{
int i = 0,len = 0;
char ans[inf];
for(i = 1;i<=n;i++)
len += mystrcmp(ans,len,s[a[i]]);
return len;
}
int main()
{
//freopen("in.in", "r", stdin);
//freopen("out.txt","w",stdout);
int t = 0,k = 0;
int i = 0,ans = 0;
scanf("%d",&t);
for(k = 1;k<=t;k++)
{
ans = inf;
printf("Case %d: ",k);
scanf("%d",&n);
for(i = 1;i<=n;i++)
{
scanf("%s",s[i]);
a[i] = i;
}
do
{
ans = min(ans,judge());
}while(next_permutation(a+1,a+n+1));
printf("%d\n",ans);
}
return 0;
}