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.
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 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.
51 2 3 4 31 2 5 4 5
1 2 5 4 3
54 4 2 3 15 4 5 3 1
5 4 2 3 1
41 1 3 41 4 3 4
1 2 3 4
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;}
- 16CF2--1002--(dfs)
- 16CF2--1001--(dfs?)
- 16CF2--1003
- CF2 A + B + C
- (CF2,包含尺度变化)tracker_ensemble程序简介
- ZOJ 1002 firenet ( DFS)
- Hierarchical Convolutional Features for Visual Tracking(CF2)论文翻译
- ZOJ 1002 Fire Net(DFS啊 )
- ZOJ Problem Set - 1002(DFS)
- HDU 4090 GemAnd Prince (DFS+BFS)/(DFS+DFS)
- (一般)POJ-2718 DFS套DFS
- zoj 1002 dfs
- zoj 1002 DFS
- zoj 1002 dfs()
- pku 1011(dfs)
- poj1321(dfs)
- 1691(dfs + 回溯)
- 【hdu】Zipper (dfs)
- hadoop启动datanode失败
- English words page four
- 关于Calendar中Maximum的成员函数
- OpenCV学习之Log-Polar极坐标变换
- 循环的试题一
- 16CF2--1002--(dfs)
- Eclipse 下找不到或无法加载主类的解决办法
- MySQL定期分析检查与优化表
- 数据库总结(二)
- Android多渠道打包
- JVM内存管理和GC机制
- 畅通工程再续||HDU1875
- Python在操作系统层面上的操作
- MFC和Qt分别使用Qt生成的Dll。