UVALive 5027 二分图 EK
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:
- S1S2, S2S3, S3S1, the score is 1+3+0 = 4
- S1S2, S2S1, S3S3, the score is 1+0+0 = 1
- S1S3, S3S1, S2S2, the score is 0+0+0 = 0
- S1S3, S3S2, S2S1, the score is 0+3+0 = 3
- S1S1, S2S2, S3S3, the score is 0+0+0 = 0
- 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 ;
}
UVALive 5027 二分图 EK的更多相关文章
- Tracer Deployment UVALive - 8271 二分图匹配
复习二分图又想起了这道题,裸的二分图匹配,直接匈牙利算法就可以了,mark一下这个比较好用的稠密图匈牙利算法模板 题目:题目链接 AC代码: #include <iostream> #in ...
- 二分图的最大匹配——最大流EK算法
序: 既然是个图,并且求边数的最大值.那么这就可以转化为网络流的求最大流问题. 只需要将源点与其中一子集的所有节点相连,汇点与另一子集的所有节点相连,将所有弧的流量限制置为1,那么最大流 == 最大匹 ...
- 网络(最大)流初步+二分图初步 (浅谈EK,Dinic, Hungarian method:]
本文中 N为点数,M为边数: EK: (brute_force) : 每次bfs暴力找到一条增广路,更新流量,代码如下 : 时间复杂度:O(NM²): #include<bits/stdc++ ...
- 训练指南 UVALive - 4043(二分图匹配 + KM算法)
layout: post title: 训练指南 UVALive - 4043(二分图匹配 + KM算法) author: "luowentaoaa" catalog: true ...
- 训练指南 UVALive - 3523 (双联通分量 + 二分图染色)
layout: post title: 训练指南 UVALive - 3523 (双联通分量 + 二分图染色) author: "luowentaoaa" catalog: tru ...
- UVALive 5033 I'm Telling the Truth 二分图最大匹配(略有修改)
I - I'm Telling the Truth Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu ...
- UVALive 5903 Piece it together(二分图匹配)
给你一个n*m的矩阵,每个点为'B'或'W'或'.'.然后你有一种碎片.碎片可以旋转,问可否用这种碎片精确覆盖矩阵.N,M<=500 WB <==碎片 W 题目一看,感觉是精确覆盖(最近 ...
- UVALive 3415 Guardian of Decency(二分图的最大独立集)
题意:老师在选择一些学生做活动时,为避免学生发生暧昧关系,就提出了四个要求.在他眼中,只要任意两个人符合这四个要求之一,就不可能发生暧昧.现在给出n个学生关于这四个要求的信息,求老师可以挑选出的最大学 ...
- UVALive 4043 Ants(二分图完美匹配)
题意:每个蚁群有自己的食物源(苹果树),已知蚂蚁靠气味辨别行进方向,所以蚁群之间的行动轨迹不能重叠.现在给出坐标系中n个蚁群和n棵果树的坐标,两两配对,实现以上要求.输出的第 i 行表示第 i 个蚁群 ...
随机推荐
- Friendly Date Ranges-freecodecamp算法题目
Friendly Date Ranges 1.要求 把常见的日期格式如:YYYY-MM-DD 转换成一种更易读的格式. 易读格式应该是用月份名称代替月份数字,用序数词代替数字来表示天 (1st 代替 ...
- SQL关于删除的三个语句:DROP、TRUNCATE、 DELETE 的区别。
DROP: DROP TABLE test; 删除表test,并释放空间,将test删除的一干二净. TRUNCATE: TRUNCATE test; 删除表test里的内容,并释放空间,但不删除表的 ...
- docker-compose 构建mongodb并导入基础数据示例
使用docker-compose构建mongodb服务并导入基础数据示例. 1.文件目录结构 ——mongo/ |——docker-compose.yml |——mongo-Dockerfile |— ...
- Ansible学习 Patterns
Ansible中ad-hoc命令格式如下:ansible <pattern_goes_here> -m <module_name> -a <arguments>,P ...
- vue本人常用插件汇总(常更新)
1. 移动端UI插件 mint-ui http://mint-ui.github.io/#!/zh-cn 2.vue状态管理vuex,持久化插件:vuex-persist https://github ...
- php+MySQL(存储过程) +yii2完整的增删改查
1在MySQL中创建存储过程 a 我将添加和修改 作为 一起 ), ), ), )) BEGIN FROM t_boss_role WHERE id = _id) THEN UPDATE t_boss ...
- Requests库:python实现的简单易用的http库
1.get请求: get(url, params, headers) 2.json 解析 3.content 获取二进制内容 4.headers 添加 5.post请求:post(url,data,h ...
- 財務会計管理(FI&CO)
FI(財務会計)系のSAP DBテーブル.随時更新していきます. [勘定コードマスタ]SKA1: 勘定コードマスタ(勘定コード表データ)SKB1: 勘定コードマスタ(会社コードデータ)SKAT: テキ ...
- 笔记-python-built-in functions-eval,exec,compile
笔记-python-built-in functions-eval,exec,compile 1. python代码执行函数 有时需要动态改变代码,也就是说代码需要是字符串格式,然后在按需要 ...
- JVM——九大工具助你玩转Java性能优化
本文转载自 http://www.importnew.com/12324.html 本文由 ImportNew - 陈 晓舜 翻译自 idrsolutions.欢迎加入翻译小组.转载请参见文章末尾的要 ...