数字反演

Description
While Patrick was gone shopping, Spongebob decided to play a little trick on his friend. The naughty Sponge browsed through Patrick's personal stuff and found a sequence a1,?a2,?...,?am of length m, consisting of integers from 1 to n, not necessarily distinct. Then he picked some sequence f1,?f2,?...,?fn of length n and for each number ai got number bi?=?fai. To finish the prank he erased the initial sequence ai.

It's hard to express how sad Patrick was when he returned home from shopping! We will just say that Spongebob immediately got really sorry about what he has done and he is now trying to restore the original sequence. Help him do this or determine that this is impossible.

Input
The first line of the input contains two integers n and m (1?≤?n,?m?≤?100?000) — the lengths of sequences fi and bi respectively.
The second line contains n integers, determining sequence f1,?f2,?...,?fn (1?≤?fi?≤?n).
The last line contains m integers, determining sequence b1,?b2,?...,?bm(1?≤?bi?≤?n).

Output
Print "Possible" if there is exactly one sequence ai, such that bi?=?fai for all i from 1 to m. Then print m integers a1,?a2,?...,?am.
If there are multiple suitable sequences ai, print "Ambiguity".
If Spongebob has made a mistake in his calculations and no suitable sequence ai exists, print "Impossible".
Sample Input
Input
3 3
3 2 1
1 2 3
Output
Possible
3 2 1 
Input
3 3
1 1 1
1 1 1
Output
Ambiguity
Input
3 3
1 2 1
3 3 3
Output
Impossible
Hint
In the first sample 3 is replaced by 1 and vice versa, while 2 never changes. The answer exists and is unique.
In the second sample all numbers are replaced by 1, so it is impossible to unambiguously restore the original sequence.

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;
        }
    }
}


你可能感兴趣的:(容器)