Toph is playing a card game. She has n𝑛 cards and each card has a unique number of 1,2⋯𝑛. In this game, Toph can operate the deck of the cards. We may wish to assume that the cards from the top to the bottom of the deck are 𝑝1,𝑝2,⋯𝑝𝑛 (a permutation), then each operation must be one of the following two cases:
- Place the top card at the bottom of the deck, that is, change the order of the deck into p2,𝑝3⋯𝑝𝑛,𝑝1.
- Place the second card from the top at the bottom of the deck, that is, change the order of the deck into p1,𝑝3⋯𝑝𝑛,𝑝2
Now, you know that the initial order(from top to bottom) of Toph’s deck is 𝑎1,𝑎2,⋯𝑎𝑛, and Toph wants to change the order of the deck into𝑏1,𝑏2,⋯𝑏𝑛 after some operations. Please construct the operation sequence to help Toph implement the change.
Toph has no patience. So the number of operations should not exceed n2𝑛2.
Input
The first line contains an integer T𝑇 , indicating the number of test cases.
For each test case:
- The first line contains an integer, n(3≤n≤1000)𝑛(3≤𝑛≤1000), indicating the number of Toph’s cards.
- The second line contains n𝑛 integers𝑎1,𝑎2,⋯𝑎𝑛, a permutation indicating the order of the deck initially.
第二行包含 n𝑛 整数𝑎1,𝑎2,⋯𝑎𝑛 ,表示甲板最初顺序的排列。 - The third line contains n𝑛 integers 𝑏1,𝑏2,⋯𝑏𝑛, a permutation indicating the order of the deck want to make.
It is guaranteed that the sum of n𝑛 in 𝑇 test cases is not exceed 10001000.
Output
For each test case:
-
Output a line, which contains a string 𝑠1𝑠2…𝑠𝑘(𝑠𝑖∈{1,2}, 1≤𝑖≤𝑘) as your operation sequence. The length of the string should not exceed 𝑛2, or you will get “Wrong Answer”.
-
If there are multiple solutions, output any of them.
Examples
Inputcopy Outputcopy 2
1
3
112212
1 2 3
2 3 1
4
1 2 3 4
2 1 3 4
Note
If 𝑎1,𝑎2⋯𝑎𝑛 and 𝑏1,𝑏2⋯𝑏𝑛 are the same in a test case, outputing an empty string is ok. But you should output an empty line in this situation.
DO NOT add extra space at the end of lines, or you will get “Wrong Answer” verdict.
分析
思维打开,题目要求不超过n^2。我们根据自己知识储备,可以联想到这道题可以用冒泡排序来进行模拟,首先回顾冒泡排序,(从左到右,相邻元素进行比较。每次比较一轮,就会找到序列中最大的一个或最小的一个。这个数就会从序列的最右边冒出来)在回到这个题,我们可以将目标顺序的下标标记出来,利用下标来算得答案。
手动模拟:
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int N=1e5+5;
int a[N],b[N],c[N];
void sovle()
{ int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=14;i<n;i++)
{ cin>>b[i];
c[b[i]]=i;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-1;j++)
{
if(c[a[j]]>c[a[j+1]])
{ cout<<"2";
swap(a[j],a[j+1]);
}
else
cout<<"1";
}
cout<<endl;
return 0;
}
}
signed main()
{
IOS;
int t;
cin>>t;
while(t--)
{
solve();
}
return 0;
}