题解:P13986 [PO Final 2023] 降重 / Synonyms

· · 题解

题目传送门

思路:

对于每个瑞典语单词,找到所有可能的外语翻译。
对于每个外语翻译,找到所有可能的瑞典语回译。
选择与原词不同的回译词(如果存在)。

步骤:

使用两个数组 swfo 分别存储瑞典语和外语单词。
对于文本中的每个单词,遍历查找其外语翻译。
对于找到的每个外语翻译,查找是否有其他瑞典语单词对应此外语。
如果找到不同的回译词,选择它作为替换。

CODE:

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
string sw[N],fo[N],text[N];
int main(){
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>sw[i]>>fo[i];
    cin>>m;
    for(int i=0;i<m;i++)
        cin>>text[i];
    for(int i=0;i<m;i++){
        string word=text[i],ans=word;
        for(int j=0;j<n;j++){
            if(sw[j]==word){
                for(int k=0;k<n;k++)
                    if(fo[k]==fo[j]&&sw[k]!=word){
                        ans=sw[k];
                        break;
                    }
                if(ans!=word)
                    break;
            }
        }
        cout<<ans<<" ";
    }
    return 0;
}