hdu 5495 LCS
来源:互联网 发布:c语言中double 编辑:程序博客网 时间:2024/06/05 14:28
很有意思的一题 怎么也想不到思路 考虑dp,搜索都做不了;
看了题解;
有一种很巧妙的方法:
例如两列数据分别是
1 3 2 4
3 2 4 1
那么1->3->2->4->1 正好构成一个循环;
可以把环里的其中一个数给拆开,那么LCS最大肯定能到n-1;
假如环的长度只有1,那么肯定能构造出来长度为1的LCS;
所以答案就是 所有长度大于1的环的长度-1相加,并加上所有长度为1的环;
可以建边之后用搜索来做,也可以用并查集来做;
也可以保存好原来数的位置,简单寻找一下就行;
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int a[100005],b[100005];int c[100005];int vis[100005];int main(){ int T; scanf("%d",&T); while(T--) { memset(vis,0,sizeof(vis)); int n; scanf("%d",&n); int i,j; for(i=1;i<=n;i++) { scanf("%d",&a[i]); c[a[i]]=i; } for(i=1;i<=n;i++) scanf("%d",&b[i]); int ans=0; for(i=1;i<=n;i++) { if(!vis[a[i]]) { int now=b[i]; int x=a[i]; ans++; int t=0; while(now!=x) { vis[a[c[now]]]=1; now=b[c[now]]; t++; if(t>1) ans++; } } } printf("%d\n",ans); } return 0;}
0 0
- HDU 5495 LCS
- hdu 5495 LCS
- HDU 5495 LCS
- hdu 5495 LCS dfs
- hdu 5495 LCS 置换
- HDU 5495 LCS
- hdu 5495 LCS(贪心)
- HDU 5495:LCS
- hdu 5495 LCS
- HDU 5495 LCS (置换)
- hdu 5495 LCS 置换群
- HDU 5495 LCS (并查集判环)
- hdu(5495)——LCS
- hdu 5495 LCS (bestcoder #58 1002)
- HDU 5495 LCS (置换群)
- HDU 5495 LCS(置换群)
- HDU-5495 LCS(最长公共子序列)
- hdu 1501 Zipper--LCS
- 虚幻4 各个平台的入口函数----------Launch模块
- HDU1241-经典dfs
- How to use 'cp' command to exclude a specific directory?
- 关于QtGraphics编程的几点经验总结_qgraphicsscene
- Apple quietly slips WebRTC audio, video into Safari's WebKit spec
- hdu 5495 LCS
- Android API Guides---App Manifest <supports-screens> <uses-configuration>
- Java并发
- 数据结构(c)——线性表:顺序表和链式表
- java基础(1)--java.math.BigDecimal类的用法
- List遍历:for,foreach还是Iterator?
- c语言结构体复习笔记
- URL shortner
- 华为P9遭疯抢,首发3分钟售罄;苹果汽车概念图遭权威杂志曝光引热议;中科大机器人太逼真