题目链接:

LCS

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 818    Accepted Submission(s): 453

Problem Description
You are given two sequence {a1,a2,...,an} and {b1,b2,...,bn}. Both sequences are permutation of {1,2,...,n}. You are going to find another permutation {p1,p2,...,pn} such that the length of LCS (longest common subsequence) of {ap1,ap2,...,apn} and {bp1,bp2,...,bpn} is maximum.
 
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:

The first line contains an integer n(1≤n≤105) - the length of the permutation. The second line contains n integers a1,a2,...,an. The third line contains nintegers b1,b2,...,bn.

The sum of n in the test cases will not exceed 2×106.

 
Output
For each test case, output the maximum length of 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
 
题意:
 
问把数列重新排一下的LCS的长度是多少;
 
思路:
 
可以发现把置换分成循环后除长度为一的循环外,每个循环都可以变换最后形成l-1的LCS,所以就好了;
 
AC代码:
 
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
int n,a[maxn],b[maxn],pos[maxn],vis[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),pos[a[i]]=i,vis[i]=0;
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
int ans=0;
for(int i=1;i<=n;i++)
{
if(!vis[b[i]])
{
vis[b[i]]=1;
int len=0,fa=b[i],p;
while(1)
{
p=pos[fa];
fa=b[p];
len++;
if(vis[fa])break;
vis[fa]=1;
}
if(len==1)ans++;
else ans+=len-1;
}
}
printf("%d\n",ans);
}
return 0;
}

  

 

hdu-5495 LCS(置换)的更多相关文章

  1. hdu 5495 LCS 水题

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

  2. hdu 5495 LCS

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

  3. hdu 5495 LCS(并查集)

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

  4. 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 ...

  5. HDU 5495:LCS

    LCS  Accepts: 127  Submissions: 397  Time Limit: 6000/3000 MS (Java/Others)  Memory Limit: 65536/655 ...

  6. hdu 1423(LCS+LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 好坑啊..还有公共串为0时的特殊判断,还有格式错误..看Discuss看知道除了最后一组测试数据 ...

  7. hdu 1503 LCS输出路径【dp】

    hdu 1503 不知道最后怎么输出,因为公共部分只输出一次.有人说回溯输出,感觉好巧妙!其实就是下图,输出的就是那条灰色的路径,但是初始时边界一定要初始化一下,因为最第一列只能向上走,第一行只能向左 ...

  8. hdu 1503, LCS variants, find a LCS, not just the length, backtrack to find LCS, no extra markup 分类: hdoj 2015-07-18 16:24 139人阅读 评论(0) 收藏

    a typical variant of LCS algo. the key point here is, the dp[][] array contains enough message to de ...

  9. Advanced Fruits(HDU 1503 LCS变形)

    Advanced Fruits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. 使用PHP搭建书虫网站

    年前开始了一个新项目,书虫项目的启动,项目组里面我是负责搭建网站的.以前听说过PHP的大名,就是没怎么看过,只能从一无所知开始了. 来自PHP手册的介绍:PHP, 即“PHP: Hypertext P ...

  2. Java 上传文件到 SFTP 抛异常 java.lang.NoClassDefFoundError: Could not initialize class sun.security.ec.SunEC 的解决办法

    最近从 Op 那里报来一个问题,说是SFTP上传文件不成功.拿到的 Exception 如下: Caused by: java.lang.NoClassDefFoundError: Could not ...

  3. 为阿里云存储开发的PHP PEAR 包:Services_Aliyun_OSS

    阿里云开放存储服务 OSS:用于存储图片.apk等静态资源,使用阿里云带宽,不占用开发者服务器带宽. 阿里云官方PHP SDK: http://aliyun.com/product/oss/#help ...

  4. Access sql语句创建表及字段类型

    创建一张空表: Sql="Create TABLE [表名]" 创建一张有字段的表: Sql="Create TABLE [表名]([字段名1] MEMO NOT NUL ...

  5. [Java] JDK 系统环境变量设置 bat

    @echo off set regpath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environmen ...

  6. Android项目实战(五):TextView自适应大小

    对于设置TextView的字体默认大小对于UI界面的好看程度是很重要的,小屏幕设置的文字过大或者大屏幕设置的文字过小都造成UI的不美观 现在就让我们学习自适应大小的TextView控件,即当文字长度变 ...

  7. IOC基础

    Ioc-Inversion of Control,即"控制反转",不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象 ...

  8. android 短信助手demo

    关于意图Intent: 显式意图:必须指定要激活的组件的完整包名和类名(应用程序之间耦合在一起) 一般激活自己应用的组件的时候采用显式意图 隐式意图:只需要指定动作和数据就可以(好处是应用程序之间没有 ...

  9. const,static,extern简介(重要)

    一.const与宏的区别(面试题): const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 编译时刻:宏是预编译(编译之前处理),const是编译 ...

  10. tp_link路由器 重新设置

    当打不开网站,而提示让我们登录电信猫时,通常是由于突然断电导致的路由器程序错误.重新路由器设置即可.     1.路由器恢复出厂设置       2.用网线分别连接电脑和路由器的LAN口.(也就是将 ...