C - Card Game

Crawling in process...Crawling failedTime Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu

Description

 

 

Jimmy invents an interesting card game. There are N cards, each of which contains a string Si. Jimmy wants to stick them into several circles, and each card belongs to one circle exactly. When sticking two cards, Jimmy will get a score. The score of sticking two cards is the longest common prefix of the second card and the reverse of the first card. For example, if Jimmy sticks the card S1 containing ``abcd" in front of the card S2 containing ``dcab", the score is 2. And if Jimmy sticks S2 in front of S1, the score is 0. The card can also stick to itself to form a self-circle, whose score is 0.

For example, there are 3 cards, whose strings are S1=``ab", S2=``bcc", S3=``ccb". There are 6 possible sticking:

  1. S1S2,     S2S3,     S3S1, the score is 1+3+0 = 4
  2. S1S2,     S2S1,     S3S3, the score is 1+0+0 = 1
  3. S1S3,     S3S1,     S2S2, the score is 0+0+0 = 0
  4. S1S3,     S3S2,     S2S1, the score is 0+3+0 = 3
  5. S1S1,     S2S2,     S3S3, the score is 0+0+0 = 0
  6. S1S1,     S2S3,     S3S2, the score is 0+3+3 = 6

So the best score is 6.

Given the information of all the cards, please help Jimmy find the best possible score.

Input

There are several test cases. The first line of each test case contains an integer N(1N200). Each of the next N lines contains a string Si. You can assume the strings contain alphabets ('a'-'z', 'A'-'Z') only, and the length of every string is no more than 1000.

Output

Output one line for each test case, indicating the corresponding answer.

Sample Input

3
ab
bcc
ccb
1
abcd

Sample Output

6
0
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
char str[][];
int lx[],ly[];
int sx[],sy[];
const int inf=;
int w[][];
int LINK[];
int ans;
int n; int dmin;
bool Find(int u)
{
sx[u] =;
for(int v = ;v <= n;v ++)
{
if(!sy[v])
{
int t = lx[u] + ly[v] - w[u][v];
if(t)
{
if(dmin > t)
dmin = t;
}
else
{
sy[v] = true;
if(LINK[v] == - || Find(LINK[v]))
{
LINK[v] = u;
return true;
}
}
}
}
return false;
} int KM(){
for(int i = ;i <= n;i ++)
{
for(int j = ;j <= n;j ++)
if(lx[i] < w[i][j])
lx[i] = w[i][j];
} memset(LINK,-,sizeof(LINK)); for(int i = ;i <= n;i ++)
{
while(true)
{ memset(sx,,sizeof(sx));
memset(sy,,sizeof(sy));
dmin = inf;
if(Find(i))
break;
for(int j = ;j <= n;j ++)
{
if(sx[j])
lx[j] -= dmin;
if(sy[j])
ly[j] += dmin;
}
}
}
for(int i = ;i <= n;i ++)
ans += w[LINK[i]][i];
return ans;
} int main(){ while(scanf("%d",&n)!=EOF){
getchar();
memset(str,,sizeof(str));
memset(lx,,sizeof(lx));
memset(ly,,sizeof(ly));
memset(w,,sizeof(w));
for(int i=;i<=n;i++){
scanf("%s",str[i]);
getchar();
} for(int i=;i<=n;i++){
for(int j=;j<=n;j++){ int num=;
if(i==j){
w[i][j]=;
continue;
}
int len1=strlen(str[i]);
int len2=strlen(str[j]);
int temp1=len1-,temp2=;
while(temp1>=&&temp2<=len2-&&str[i][temp1]==str[j][temp2]){ num++;
temp1--;
temp2++; }
w[i][j]=num; }
}
ans=;
ans=KM();
printf("%d\n",ans);
}
return ;
}

最新文章

  1. OBS MAC 系统开发(基于mac OS X 10.12)
  2. yum
  3. [LeetCode]413 Arithmetic Slices
  4. LeetCode &quot;Russian Doll Envelopes&quot;
  5. iOS中UITableView数据源刷新了,但tableview当中的cell没有刷新
  6. Java_Ant 详解
  7. CSS根据屏幕分辨率应用相应样式
  8. C#中自定义消息,与MFc对比
  9. 【mongodb】 需求
  10. jboss eap6出现Tags_$$_javassist_26 cannot be cast to javassist.util.proxy.ProxyObject的解决办法
  11. pdb文件部分解释
  12. 【开源java游戏框架libgdx专题】-07-文件处理
  13. 管理TEMP数据
  14. Swift 闭包表达式
  15. 为什么CPU需要时钟这种概念?
  16. x宝23大洋包邮的老式大朝华MP3播放器简单评测
  17. DNS: Internet’s Directory
  18. background-color:transparent
  19. poj1742 多维背包
  20. Node.js进击基础一(5-11事件模块)

热门文章

  1. 【洛谷P1880】[NOI1995]石子合并
  2. linux数据库copy方法
  3. mybatis两级缓存原理剖析
  4. GNU 关闭 MMU 和 Icache 和 Dcache
  5. linux下csv导出文件中文乱码问题
  6. JQuery制作网页—— 第四章JavaScript对象及初识面向对象
  7. 传输控制协议(TCP)
  8. python__系统 : 进程
  9. 遗传算法 | Java版GA_TSP(我的第一个Java程序)
  10. 栈和队列&amp;前缀,中缀,后缀