CF 501B Misha and Changing Handles 模拟,水题

    xiaoxiao2021-03-25  87

    题目链接:这里 题意:按照时间顺序给了n对新旧字符串,其中如果满足a->b&&b->c那么最后只会存在a->c,然后让输出最后字符串对。 解法:水题,按照题意模拟就可以了,用map

    //CF 501B #include <bits/stdc++.h> using namespace std; const int maxn = 1010; map <string, string> mp; int n; string s1, s2; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for(int i = 0; i < n; i++){ cin >> s1 >> s2; if(i == 0) mp[s1] = s2; else{ int flag = 0; for(map <string, string> :: iterator it = mp.begin(); it != mp.end(); it++){ if(it->second == s1){ mp[it->first] = s2; flag = 1; } } if(flag == 0) mp[s1] = s2; } } cout << mp.size() << endl; for(map <string, string> :: iterator it = mp.begin(); it != mp.end(); it++){ cout << it -> first << " " << it -> second << endl; } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-23958.html

    最新回复(0)