hdu 5495 LCS 水题
2024-10-05 23:48:13
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\}{a1,a2,...,an}和\{b_1,b_2,...,b_n\}{b1,b2,...,bn}. 他们都是11到nn的一个排列. 你需要找到另一个排列\{p_1,p_2,...,p_n\}{p1,p2,...,pn}, 使得序列\{a_{p_1},a_{p_2},...,a_{p_n}\}{ap1,ap2,...,apn}和\{b_{p_1},b_{p_2},...,b_{p_n}\}{bp1,bp2,...,bpn}的最长公共子序列的长度最大.
Input
输入有多组数据, 第一行有一个整数TT表示测试数据的组数. 对于每组数据: 第一行包含一个整数n (1 \le n \le 10^5)n(1≤n≤105), 表示排列的长度. 第2行包含nn个整数a_1,a_2,...,a_na1,a2,...,an. 第3行包含nn个整数 b_1,b_2,...,b_nb1,b2,...,bn. 数据中所有nn的和不超过2 \times 10^62×106.
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);
}
}
最新文章
- super
- 每日构建【Daily Build Using CruiseControl.NET and MSBuild】(转载)
- Aeroplane chess(HDU 4405)
- web首页设置如下代码可判断用户是用什么设备登录的?
- Can&#39;t connect to local MySQL server through socket &#39;/tmp/mysql.sock&#39; (111)
- D - 粉碎叛乱F - 其他起义
- wpf 寻找某个控件下的子控件
- asp.net core 开发的https证书服务-agilelabs.net
- Python学习之list有序集合
- Android远程桌面助手(Build 0737)
- thinkphp 响应对象
- c/c++ 线性表之单向循环链表
- java 路径分隔符自动适配
- Git学习笔记4
- JavaEE笔记(十)
- laravel 批量更新
- 封装ajax函数
- C语言 string::size_type类型
- JavaScript 学习(一)
- UserUI程序详解