In the third sample fi?≠?3 for all i, so no sequence ai transforms into such bi and we can say for sure that Spongebob has made a mistake.
题目描述:有等式(Bi = Fai),给你一串数字F1,F2,....Fi以及另一串数字B1,B2...Bi让你判断是否能根据这两串数据求出Ai
例如:3,2,1,
1,2,3
F1 = 3 = B3,即有i= 3,所以A3 = 1;
F2 = 2 = B2,即有i= 2,所以A2 = 2;
F3 = 1 = B1,即有i= 1,所以A1 = 3;
思路:用set保存两个数列可以去重的同时方便查询
当 f 数列中出现两个相同的数字时则有多种解;
否则在存储f的set中查找b数列中的数,找到则可以算出对应的ai,没有则无解;
感觉a题的时候想复杂了,没必要用到结构体,直接一个数组就能保存序号;
#include
#include
#include
using namespace std;
typedef struct Num{
int v,n;
bool operator < (const Num &n) const
//重载小于号,按从大到小顺序排列
{
if(v != n.v)
{
return v > n.v;
}
else
{
return v < n.v;
}
}
}Num;
int main() {
set f;
set b;
int k[100005]= {0};
int fi[100005] = {0},bi[100005] = {0};
int l = 0;
bool flag = true;
int n,m;
Num temp;
scanf("%d%d",&n,&m);
for(int i = 0;i < n;i++) {
scanf("%d",&(temp.v));
temp.n = i + 1;
if(f.count(temp)) {
k[l++] = temp.v;
}
else f.insert(temp);
}
for(int j = 0;j < m;j++) {
scanf("%d",&(temp.v));
bi[j] = temp.v;
temp.n = j + 1;
b.insert(temp);
}
//读入数据
for(int j = 0;j < l;j++) {
Num t;
t.v =k[j];
t.n = 0;
if(b.count(t)){
flag = false;
}
}
set::iterator fIte = f.begin(),bIte = b.begin();
while(fIte != f.end() && bIte != b.end()) {
if(f.count(*bIte)) {
fIte++;
bIte++;
}
else
{
printf("Impossible");
return 0;
}
}
if(fIte == f.end() && bIte != b.end()) {
printf("Impossible");
return 0;
}
else {
if(!flag) {
printf("Ambiguity");
return 0;
}
else {
printf("Possible\n");
for(int i = 0;i < m;i++) {
Num t;
t.v = bi[i];
t.n = 0;
printf("%d ",(*f.find(t)).n );
}
return 0;
}
}
}