16CF2--1002--(dfs)

来源:互联网 发布:手机动漫壁纸软件 编辑:程序博客网 时间:2024/06/08 02:53

Sengoku still remembers the mysterious "colourful meteoroids" she discovered with Lala-chan when they were little. In particular, one of the nights impressed her deeply, giving her the illusion that all her fancies would be realized.

On that night, Sengoku constructed a permutation p1, p2, ..., pn of integers from 1 to n inclusive, with each integer representing a colour, wishing for the colours to see in the coming meteor outburst. Two incredible outbursts then arrived, each with nmeteorids, colours of which being integer sequences a1, a2, ..., an and b1, b2, ..., bnrespectively. Meteoroids' colours were also between 1 and n inclusive, and the two sequences were not identical, that is, at least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Well, she almost had it all — each of the sequences a and b matched exactly n - 1elements in Sengoku's permutation. In other words, there is exactly one i (1 ≤ i ≤ n) such that ai ≠ pi, and exactly one j (1 ≤ j ≤ n) such that bj ≠ pj.

For now, Sengoku is able to recover the actual colour sequences a and b through astronomical records, but her wishes have been long forgotten. You are to reconstruct any possible permutation Sengoku could have had on that night.

Input

The first line of input contains a positive integer n (2 ≤ n ≤ 1 000) — the length of Sengoku's permutation, being the length of both meteor outbursts at the same time.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the sequence of colours in the first meteor outburst.

The third line contains n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ n) — the sequence of colours in the second meteor outburst. At least one i (1 ≤ i ≤ n) exists, such that ai ≠ bi holds.

Output

Output n space-separated integers p1, p2, ..., pn, denoting a possible permutation Sengoku could have had. If there are more than one possible answer, output any one of them.

Input guarantees that such permutation exists.

Example
Input
51 2 3 4 31 2 5 4 5
Output
1 2 5 4 3
Input
54 4 2 3 15 4 5 3 1
Output
5 4 2 3 1
Input
41 1 3 41 4 3 4
Output
1 2 3 4
Note

In the first sample, both 1, 2, 5, 4, 3 and 1, 2, 3, 4, 5 are acceptable outputs.

In the second sample, 5, 4, 2, 3, 1 is the only permutation to satisfy the constraints.


题意:

给出n时字符序列长度,给出序列a,b,两序列至少有一个相对字符是不同的,两序列均与c序列仅有一个字符不同,让找出c,

解题思路:

记录下a,b不同的字符对应的下标和总个数,对于每个不同字符进行深搜,

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int n,num,a[1005],b[1005],c[1005],d[1005]; //c记录第i个不同处的下标,d是结果bool flag,ok[1005]; //记录字符使用状态bool work() //如果两字符串与d均仅有一处不同,那么d就是结果{    int n1=0,n2=0;    for(int i=1;i<=n;i++)    {        if(a[i]!=d[i]) n1++;        if(b[i]!=d[i]) n2++;    }    if(n1!=1||n2!=1) return false;    return true;}void dfs(int i){    if(flag) return;    for(int j=1;j<=n;j++)  //用不同的数尝试替换第i处    {        if(ok[j]) continue;        d[c[i]]=j;  //放入d中        ok[j]=true;        if(i==num)        {            if(work())            {                flag=true;                return;            }        }        else        {            dfs(i+1);            if(flag) return;        }        ok[j]=false;    }}int main(){    scanf("%d",&n);    int i;    for(i=1;i<=n;i++)    {        scanf("%d",&a[i]);    }    for(i=1;i<=n;i++)    {        scanf("%d",&b[i]);    }    num=0;    memset(ok,false,sizeof(ok));    for(i=1;i<=n;i++)    {        if(a[i]!=b[i])        {            num++;            c[num]=i;            d[i]=0;        }        else        {            d[i]=a[i];      //将d初始化,相同处放入d中,不同处记为0            ok[d[i]]=true;  //ok数组初始化        }    }    flag=false;  //记录是否找到结果    dfs(1);    for(i=1;i<=n;i++)    {        printf("%d ",d[i]);    }    return 0;}


原创粉丝点击