HDU 5495 LCS (置换群)
来源:互联网 发布:wps表格怎么刷新数据 编辑:程序博客网 时间:2024/04/30 14:10
你有两个序列{a1,a2,...,an}和{b1,b2,...,bn}. 他们都是1到n的一个排列. 你需要找到另一个排列{p1,p2,...,pn}, 使得序列{ap1,ap2,...,apn}和{bp1,bp2,...,bpn}的最长公共子序列的长度最大.
输入描述
输入有多组数据, 第一行有一个整数T表示测试数据的组数. 对于每组数据:第一行包含一个整数n(1≤n≤105), 表示排列的长度. 第2行包含n个整数a1,a2,...,an. 第3行包含n个整数 b1,b2,...,bn.数据中所有n的和不超过2×106.
输出描述
对于每组数据, 输出LCS的长度.
输入样例
231 2 33 2 161 5 3 2 6 43 6 2 4 5 1
输出样例
24
变量名wa了N发
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=1e5+100;int a[maxn],vis[maxn],pre[maxn];int main(){ int tt,n,i,j,ans,t; scanf("%d",&tt); while(tt--) { memset(vis,0,sizeof(vis)); scanf("%d",&n); ans=n; for(i=1;i<=n;i++) scanf("%d",&a[i]); for(i=1;i<=n;i++) { scanf("%d",&pre[a[i]]); } for(i=1;i<=n;i++) { if(!vis[a[i]]) { if(pre[a[i]]==a[i]) continue; else { t=a[i]; while(!vis[t]) { vis[t]=1; t=pre[t]; } ans--; } } } printf("%d\n",ans); } return 0;}
0 0
- HDU 5495 LCS (置换群)
- HDU 5495 LCS(置换群)
- hdu 5495 LCS 置换群
- HDU 5495 LCS (置换)
- hdu 5495 LCS 置换
- HDU5495--LCS(置换群)
- HDU 5495 置换群
- HDU 5495(置换群)
- hdoj 5495 LCS 【统计元素个数大于1的置换群数目】【LCS变形】
- hdu 5495 置换
- 【HDU 1439】Cipher(置换群)
- hdu 4259 置换群
- HDU-置换群
- hdu 置换群
- HDU 5495 LCS
- hdu 5495 LCS
- HDU 5495 LCS
- hdu 5495 LCS dfs
- hustoj的永久pending 与 永久Running & Judging解决
- [258]Add Digits
- 安装vmware-tools遇The path "" is not a valid path to the 3.8.13-44.1.1.el6uek.x86_64 kernel问题解决
- [精华] 前段时间arch/i386/kernel/head.S注解
- CF#323-DIV2-D. Once Again-暴力贪心LIS
- HDU 5495 LCS (置换群)
- 《TCP/IP详解 卷一:协议》读书笔记--RARP:逆地址解析协议
- 详解ThreadLocal模式
- Ubuntu源问题(换源软件源更新源)
- tomcat的详细安装步骤
- windows-linux
- Leetcode Clone Graph
- [LeetCode Problem]TwoSum
- 如何将PreparedStatement查询得到的结果集存储起来--方法一:采用对象数组