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 个蚁群 ...
随机推荐
- P1725 琪露诺
P1725 琪露诺 单调队列优化dp 对于不是常数转移的dp转移,我们都可以考虑单调队列转移 然而我们要把数组开大 #include<cstdio> #include<algorit ...
- cudaMallocPitch – 向GPU分配存储器
概要 cudaError_t cudaMallocPitch( void** devPtr,size_t* pitch,size_t widthInBytes,size_t height ) 说明 向 ...
- Webpack4 学习笔记五 图片解析、输出的文件划分目录
前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 webpack打包图片和划分文件路径 使用图片的方式 通过 new Image() 在 css中设置 background-imag ...
- http状态码有那些,分别代表什么意思
http1.0和2.0的区别https://blog.csdn.net/linsongbin1/article/details/54980801/ 简单版: 100 Continue ...
- Python线程创建与使用
Python多为线程编程提供了两个简单明了的模块:thread和threading,Python3中已经不存thread模块,已经被改名为_thread,实际优先使用 threading模块. 1.P ...
- hdu_5288_OO’s Sequence
OO has got a array A of size n ,defined a function f(l,r) represent the number of i (l<=i<=r) ...
- tcp服务端socket
import socket if __name__ == '__main__': # 创建tcp服务端socket tcp_server_socket = socket.socket(socket.A ...
- Shell学习——Shell分类:登录shell和非登陆shell 交互shell和非交互shell
1.从两个不同维度来划分,是否交互式,是否登录 2.交互式shell和非交互式shell 交互式模式:在终端上执行,shell等待你的输入,并且立即执行你提交的命令.这种模式被称作交互式是因为shel ...
- nuxt generate静态化后回退问题
之前线上的项目是nuxt build后的项目发布在服务器上,pm2来管理node的进程,nuxt还是运行在node的环境里. 这个方案用了半年左右,访问速度什么的确实很快,pm2管理下的node在wi ...
- js延迟加载的方式有哪些?
共有:defer和async.动态创建DOM方式(用得最多).按需异步载入js defer属性:(页面load后执行) HTML 4.01 为 <script>标签定义了 defer属性. ...