标签:algo png 技术 cstring print turn 题目 har 定义

定义map存储所有的<地址1,地址2>
第一set存放单词1的所有地址(通过查找map)
通过单词二的首地址,结合map,然后在set中查找是否存在,如果存在就是所求的地址,没有就是-1
无
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
int main() {
    int n;
    string a, b;
    cin>>a>>b>>n;
    map<string, string> common;
    set<string> c1;
    for(int i=0;i<n;i++){
        string s1, s2;
        char ch;
        cin>>s1>>ch>>s2;
        common[s1] = s2;
    }
    string _a = a;
    c1.insert(_a);
    while((_a = common[_a]) != "-1"){
        c1.insert(_a);
    }
    c1.insert(_a);
    
    string _b = b;
    if(c1.find(_b) != c1.end()){
        printf("%s", _b.c_str());
        return 0;
    }
    while((_b = common[_b]) != "-1"){
        if(c1.find(_b) != c1.end()){
            printf("%s", _b.c_str());
            return 0;
        }
    }
    printf("-1");
}标签:algo png 技术 cstring print turn 题目 har 定义
原文地址:https://www.cnblogs.com/d-i-p/p/12377175.html