字符串处理 Codeforces Round #285 (Div. 2) B. Misha and Changing Handles

 

题目传送门

 1 /*  2  题意:给出一系列名字变化,问最后初始的名字变成了什么  3  字符串处理:每一次输入到之前的找相印的名字,若没有,则是初始的,pos[m] 数组记录初始位置  4  在每一次更新时都把初始pos加上去,那么就保证更新了初始的名字,这也是唯一要思考的地方了:)  5 */  6 #include <cstdio>  7 #include <iostream>  8 #include <cstring>  9 #include <algorithm> 10 #include <cmath> 11 #include <string> 12 #include <map> 13 #include <ctime> 14 using namespace std; 15 16 const int MAXN = 1e3 + 10; 17 const int INF = 0x3f3f3f3f; 18 string old[MAXN]; 19 string change[MAXN]; 20 string ans[MAXN]; 21 int pos[MAXN]; 22 23 int main(void) //Codeforces Round #285 (Div. 2) B. Misha and Changing Handles 24 { 25 freopen ("B.in", "r", stdin); 26 27 int n; 28 while (scanf ("%d", &n) == 1) 29  { 30 memset (pos, 0, sizeof (pos)); 31 string tmp; int m = 0; 32 for (int i=1; i<=n; ++i) 33  { 34 cin >> tmp; cin >> change[i]; 35 bool ok = false; 36 for (int j=1; j<i; ++j) 37  { 38 if (tmp == change[j]) 39  { 40 ok = true; 41 ans[pos[j]] = change[i]; 42 pos[i] = pos[j]; break; 43  } 44  } 45 if (!ok) 46  { 47 old[++m] = tmp; ans[m] = change[i]; pos[i] = m; 48  } 49  } 50 51 printf ("%d\n", m); 52 for (int i=1; i<=m; ++i) 53  { 54 cout << old[i] << " " << ans[i] << endl; 55  } 56  } 57 58 // cout << "time: " << (double) clock () / CLOCKS_PER_SEC * 1000 << " ms\n"; 59 60 return 0; 61 }

 

你可能感兴趣的:(codeforces)