题解:CF599B Spongebob and Joke

· · 题解

传送门

题解

我们发现如果 b_i 在数组 f 没出现过,则输出 Impossible;如果数组 f 内有多个数相同出现,则输出 Ambiguit;否则输出 Possible。

于是,我们可以用数组 cnt 记录每个数在数组 f 出现的个数。

最后只有一个数相同出现的时候,可以用数组 pos 记录每个数在数组 f 出现的位置;输出对应答案时,看 pos_{b_i} 为什么就结束了。

AC code:

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=2e5+5;
int n,m;
int f[N],b[N];
int cnt[N];//cnt[i] 表示 i 这个数在 f 出现的个数。 
int pos[N];//pos[i] 表示 i 这个数在 f 出现的位置。 
signed main(){
    //HAPPY!
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>f[i];
        cnt[f[i]]++;
        pos[f[i]]=i; 
    }
    bool v=0;//多方案。 
    for(int i=1;i<=m;i++){
        cin>>b[i];
        if(!cnt[b[i]]){//无。 
            cout<<"Impossible\n";
            return 0;
        } 
        if(cnt[b[i]]>1){//多个数相同,无法区分。 
            v=1; 
        }
    }
    if(v){
        cout<<"Ambiguity\n";
        return 0;
    }
    cout<<"Possible\n";//只有一种方案。 
    for(int i=1;i<=m;i++){
        cout<<pos[b[i]]<<" ";
    }
    cout<<"\n";
    return 0;
}