LCS

 Accepts: 127
 Submissions: 397
 Time Limit: 6000/3000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
你有两个序列\{a_1,a_2,...,a_n\}{a​1​​,a​2​​,...,a​n​​}和\{b_1,b_2,...,b_n\}{b​1​​,b​2​​,...,b​n​​}. 他们都是11到nn的一个排列. 你需要找到另一个排列\{p_1,p_2,...,p_n\}{p​1​​,p​2​​,...,p​n​​}, 使得序列\{a_{p_1},a_{p_2},...,a_{p_n}\}{a​p​1​​​​,a​p​2​​​​,...,a​p​n​​​​}和\{b_{p_1},b_{p_2},...,b_{p_n}\}{b​p​1​​​​,b​p​2​​​​,...,b​p​n​​​​}的最长公共子序列的长度最大.
输入描述
输入有多组数据, 第一行有一个整数TT表示测试数据的组数. 对于每组数据:

第一行包含一个整数n (1 \le n \le 10^5)n(1≤n≤10​5​​), 表示排列的长度. 第2行包含nn个整数a_1,a_2,...,a_na​1​​,a​2​​,...,a​n​​. 第3行包含nn个整数 b_1,b_2,...,b_nb​1​​,b​2​​,...,b​n​​.

数据中所有nn的和不超过2 \times 10^62×10​6​​.
输出描述
对于每组数据, 输出LCS的长度.
输入样例
2
3
1 2 3
3 2 1
6
1 5 3 2 6 4
3 6 2 4 5 1
输出样例
2
4

题目中给出的是两个排列, 于是我们可以先把排列分成若干个环, 显然环与环之间是独立的. 事实上对于一个长度为l
(l > 1)l(l>1)的环,
我们总可以得到一个长度为l-1l−1的LCS,
于是这个题的答案就很明显了, 就是nn减去长度大于11的环的数目.

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; const int maxn=1e5+10;
bool vis[maxn]; struct node
{
int a,b;
}p[maxn]; bool cmp(node x,node y)
{
return x.a<y.a;
}
int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout);
int T ;
cin>>T;
while(T--)
{
memset(vis,0,sizeof(vis));
int n; scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&p[i].a);
for(int i=1;i<=n;i++) scanf("%d",&p[i].b);
sort(p+1,p+1+n,cmp);
int ans=0;
for(int i=1;i<=n;i++)
{
if(vis[i]) continue;
vis[i]=1;
if(p[i].a==p[i].b) ans++;
else
{
int pos=p[i].b;
while(!vis[pos])
{
ans++;
vis[pos]=1;
pos=p[pos].b;
}
}
}
printf("%d\n",ans);
}
//system("pause");
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

HDU 5495:LCS的更多相关文章

  1. hdu 5495 LCS

    Problem Description You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are ...

  2. HDU - 6409:没有兄弟的舞会(数学+思维)

    链接:HDU - 6409:没有兄弟的舞会 题意: 题解: 求出最大的 l[i] 的最大值 L 和 r[i] 的最大值 R,那么 h 一定在 [L, R] 中.枚举每一个最大值,那么每一个区间的对于答 ...

  3. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  4. hdu 5495 LCS 水题

    LCS Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5495 Descr ...

  5. HDU 1513 Palindrome:LCS(最长公共子序列)or 记忆化搜索

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1513 题意: 给你一个字符串s,你可以在s中的任意位置添加任意字符,问你将s变成一个回文串最少需要添加 ...

  6. HDU 1159 Common Subsequence:LCS(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 题意: 求最长公共子序列. 题解: (LCS模板题) 表示状态: dp[i][j] = max ...

  7. hdu 5495 LCS(并查集)

    Problem Description You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are ...

  8. HDU 1159:Common Subsequence(LCS模板)

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 5495 LCS (置换群)

    Sample Input231 2 33 2 161 5 3 2 6 43 6 2 4 5 1  Sample Output24 C/C++: #include <map> #includ ...

随机推荐

  1. FF获6亿美元投资九城或许比贾跃亭更着急

    互联网企业第九城市(以下简称"九城")确认,已透过旗下子公司与总部位于美国加州的法拉第未来公司签定协议,双方共同建立合资公司,在中国制造.营销及运营电动汽车.根据合资公司协议条款, ...

  2. 自定义更改 Xcode 新建 .h/.m 文件头部注释说明(文件名、工程名、作者、公司、版权等)信息

    使用 Xcode 新建工程文件时,或默认生成一套注释说明信息在 .h/.m 文件的头部,说明了创建这个文件的名称.工程名.日期.作者.公司.版权等信息 // // ___FILENAME___ // ...

  3. Python学习 —— 实现简单的爬虫

    为了加快学习python3.x,查了许多资料后写了这个脚本,这个脚本主要是爬取百度图片'东方幻想乡'的图片,但还是有很多问题存在. 下面给出代码: # 更新了一下代码 from urllib impo ...

  4. shell 脚本通过Webhook 发送消息到微信群

    代码如下: #!/bin/sh # Filename: msg.sh # # Usage msg.sh "message text" # # 1. check if missing ...

  5. vscode调试开发C/C++程序

    https://www.cnblogs.com/TAMING/p/8560253.html

  6. [aac @ ...] Specified sample format s16 is invalid or not supported

    在使用FFmpeg打开编码器的时候出现以下错误: [aac @ 000001da19fd7200] Specified sample format s16 is invalid or not supp ...

  7. package.json中一些配置项的含义

    {   "name": "webpack-demo",   "version": "1.0.0",   "de ...

  8. 一文解读CDN (转)

    如今这个移动互联网时代,越来越多的人使用手机观看视频,丰富自己的娱乐生活. 可是,大家在追剧的时候,有没有想过一个问题——为什么有时候明明自己手机的网速很快,但观看视频时,仍然卡顿? 回答这个问题之前 ...

  9. VS2010解决闪退的方法

    VS2010解决闪退的原因 前言 在利用vs2010编译器进行编写程序的时候程序结果无法看到,针对上述问题有如下两个解决方法: 方法1. 在程序结束之前(return之前)加  system(&quo ...

  10. centos7 bond双网卡

    [root@pay network-scripts]# cat ifcfg-bond0 |grep -v \#TYPE="Ethernet"PROXY_METHOD="n ...