LCS

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5495

Description

你有两个序列\{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​​​​}的最长公共子序列的长度最大.

Input

输入有多组数据, 第一行有一个整数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​​.

Output

对于每组数据, 输出LCS的长度.

Sample Input

2
3
1 2 3
3 2 1
6
1 5 3 2 6 4
3 6 2 4 5 1

Sample Output

2
4

HINT

题意

题解:

建边,a[i]->b[i]这样建边,对于其中构成的长度为l的环,我们能构造出长度为l-1的lcs

所以答案就是n-环的个数

代码:

#include<iostream>
#include<stdio.h>
#include<queue>
#include<map>
#include<algorithm>
using namespace std; int a[];
int b[];
int c[];
int vis[];
int main()
{
int n;
int t;scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
vis[i]=;
}
for(int i=;i<=n;i++)
scanf("%d",&b[i]);
for(int i=;i<=n;i++)
c[a[i]]=b[i];
int ans = n;
for(int i=;i<=n;i++)
{
int x=i;
if(vis[x])continue;
if(c[x]!=x)
{
ans--;
while(!vis[x])
{
vis[x]=;
x=c[x];
}
}
}
printf("%d\n",ans);
}
}

最新文章

  1. super
  2. 每日构建【Daily Build Using CruiseControl.NET and MSBuild】(转载)
  3. Aeroplane chess(HDU 4405)
  4. web首页设置如下代码可判断用户是用什么设备登录的?
  5. Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (111)
  6.  D - 粉碎叛乱F - 其他起义
  7. wpf 寻找某个控件下的子控件
  8. asp.net core 开发的https证书服务-agilelabs.net
  9. Python学习之list有序集合
  10. Android远程桌面助手(Build 0737)
  11. thinkphp 响应对象
  12. c/c++ 线性表之单向循环链表
  13. java 路径分隔符自动适配
  14. Git学习笔记4
  15. JavaEE笔记(十)
  16. laravel 批量更新
  17. 封装ajax函数
  18. C语言 string::size_type类型
  19. JavaScript 学习(一)
  20. UserUI程序详解

热门文章

  1. SQLServer与Oracle的数据同步(触发器trigger)
  2. bash 统计文件行数
  3. Android开发之多媒体编程之获取图片的副本
  4. shape的属性
  5. 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符
  6. git提交小结
  7. AngularJS with MVC4 CRUD
  8. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.5
  9. OD使用经验【转载】
  10. HDU 2196-Computer(树形dp)