Happy PMP is freshman and he is learning about algorithmic problems. He enjoys playing algorithmic games a lot.
One of the seniors gave Happy PMP a nice game. He is given two permutations of numbers 1 through n and is asked to convert the first one to the second. In one move he can remove the last number from the permutation of numbers and inserts it back in an arbitrary position. He can either insert last number between any two consecutive numbers, or he can place it at the beginning of the permutation.
Happy PMP has an algorithm that solves the problem. But it is not fast enough. He wants to know the minimum number of moves to convert the first permutation to the second.
Input
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the quantity of the numbers in the both given permutations.
Next line contains n space-separated integers — the first permutation. Each number between 1 to n will appear in the permutation exactly once.
Next line describe the second permutation in the same format.
Output
Print a single integer denoting the minimum number of moves required to convert the first permutation to the second.
Sample Input
3
3 2 1
1 2 3
2
5
1 2 3 4 5
1 5 2 3 4
1
5
1 5 2 3 4
1 2 3 4 5
3
题解:从初始序列中找出目标序列匹配的个数。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
#include <algorithm>
#include <queue>
#include <map>
#include <cmath>
using namespace std;
int a[200200],b[200200];
int main(){
int n;
while (~scanf("%d",&n)){
int ans=0;
for (int i=0; i<n; i++){
scanf("%d",&a[i]);
}
for (int i=0; i<n; i++){
scanf("%d",&b[i]);
}
int p=0;
for (int i=0; i<n; i++){
while (a[i]!=b[p] && p<n) p++;
if (a[i]==b[p] && p<n){
ans++;
p++;
}
if (p==n) break;
}
printf("%d\n",n-ans);
}
return 0;
}