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:
- S1
S2, S2
S3, S3
S1, the score is 1+3+0 = 4
- S1
S2, S2
S1, S3
S3, the score is 1+0+0 = 1
- S1
S3, S3
S1, S2
S2, the score is 0+0+0 = 0
- S1
S3, S3
S2, S2
S1, the score is 0+3+0 = 3
- S1
S1, S2
S2, S3
S3, the score is 0+0+0 = 0
- S1
S1, S2
S3, S3
S2, 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(1N
200). 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 个蚁群 ...
随机推荐
- Mathematica 讲座
Mathematica 讲座笔记本 [下载] 第一章 Mathematica 简介 [观看] [下载] 第二章 Mathematica 界面和编程语言 [观看] [下载] 第三章 符号运算 [观看] ...
- 移动页面请使用CSS3动画
说到动画,我们一般会使用jQuery 中的animate(); 在PC浏览器中,是很方便的,由于PC的高性能,这种不断修改DOM的做法确实不会出现太大的问题,但是在手机端就不同了. 手机上使用jQue ...
- JavaEE权限管理系统的搭建(三)--------springmvc和mabatis整合环境搭建
本节介绍如何环境的搭建和配置: 首先要在父工程引入jar包依赖: <!-- 通过属性定义指定jar的版本 --> <properties> <spring.version ...
- SqlServer中怎么删除重复的记录(表中没有id)
SqlServer中怎么删除重复的记录(表中没有id) 其实我在别的网址也查到过删除重复的记录,不知道我是我SqlServer2012版本太低还是啥原因 delete from scwhere (c# ...
- 一句话说明==和equals的区别
public class equals { public static void main(String[] args) { int x=10; int y=10; String str1=new S ...
- PHP:(一)安装并使用PHP
php的安装分为两个部分:环境安装配置和开发工具 一.集成环境安装配置 (一)安装 选择:wampserver或者xampp 我采用的是xampp. 在http://www.sourceforce.n ...
- BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)
Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 989 Solved: 660[Submit][Status][Discuss] Description ...
- mongodb多个查询语句
db.getCollection('costitems').find({"created":{"$gte":ISODate("2019-01-02T0 ...
- Windows Server 2012 搭建DHCP及远程路由访问
1.1 基础环境信息 1.2 DHCP与远程访问服务器角色安装 1.服务器管理器—>仪表板—>添加角色和功能,出现添加角色和功能向导,点击下一步 2.选择安装类型为基于角色或基 ...
- java-访问控制修饰符
访问权限 public 任何情况都可以访问 默认包 本包范围内可以访问到 protect 同一个包里的所有类所可以访问:所有子类(子类可以不和父类在同一个包)都可以访问 privat ...