luoguP2814 家谱

刚发现这是一道蓝题

emmmmm

其实没有很难

只要你知道map这个强大的东西

那么map是什么呢emmm

/*

  1. map<string,int>以字符串为下标,int为值的map
  2. map<long long,long long>以longlong为下标,数组开不下的时候用。。。

*/

map在头文件

#include<map>中

然后,就没有然后了(这句话显得很不靠谱

当数组用就行了

之后的话,这道题就是一道很简单的并查集了

说是模版也不为过

emmm没什么好讲的了

看代码

#include<cstdio>
#include<map>
#include<iostream>
using namespace std;

map<string,string>fa;

string get(string x){
    if(fa[x] == x)return x;
    return fa[x] = get(fa[x]);
}

int main(){
    string a,b;
    char ch;
    cin >> ch;
    while(ch != '$'){
        cin >> a;
        if(ch == '#'){
            b = a;
            if(fa[a] == "")fa[a] = a; 
        }
        else if(ch == '+')
        fa[a] = b;
        else cout << a << ' ' << get(a) << endl;
        cin >> ch;
    }
    return 0;
}//感觉没什么注释可写啊。。。orz【瘫

 

posted @ 2018-12-02 16:39  ./seven  阅读(139)  评论(0编辑  收藏  举报