Codeforces 501B - Misha and Changing Handles (STL)

题意

有一些人改名字,输出每个人的原名和改了之后的名字.

思路

用map记录每个人换过的名字,用set记录是不是一个新的人,如果是的话就放到数组里。

代码

  
  
  
  
  1. #include <cstdio>
  2. #include <stack>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <iomanip>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <string>
  15. #include <map>
  16. #include <cmath>
  17. using namespace std;
  18. #define LL long long
  19. #define ULL unsigned long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define F first
  26. #define S second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. #define BitCountll(x) __builtin_popcountll(x)
  35. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  36. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  37. const double PI = acos(-1.0);
  38. const int INF = 0x3f3f3f3f;
  39. const double eps = 1e-8;
  40. const int MAXN = 1000 + 10;
  41. const int MOD = 1000007;
  42. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  43. typedef pair<int, int> pii;
  44. typedef vector<int>::iterator viti;
  45. typedef vector<pii>::iterator vitii;
  46. vector<string> ans;
  47. set<string> st;
  48. map<string, string> mp;
  49. int main()
  50. {
  51. // ROP;
  52. int n, i, j;
  53. cin >> n;
  54. for (i = 0; i < n; i++)
  55. {
  56. string from, to;
  57. cin >> from >> to;
  58. st.insert(to);
  59. if (!st.count(from)) ans.PB(from);
  60. mp[from] = to;
  61. }
  62. cout << SZ(ans) << endl;
  63. for (i = 0; i < SZ(ans); i++)
  64. {
  65. string tmp = ans[i];
  66. while (mp.count(mp[tmp])) tmp = mp[tmp];
  67. cout << ans[i] << " " << mp[tmp] << endl;
  68. }
  69. return 0;
  70. }
  
  
  
  

你可能感兴趣的:(ACM,codeforces)