zoukankan      html  css  js  c++  java
  • 字符串处理 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
    ", 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
    ";
    59 
    60     return 0;
    61 }
    编译人生,运行世界!
  • 相关阅读:
    POJ 1611 The Suspects
    POJ 2001 Shortest Prefixes(字典树)
    HDU 1251 统计难题(字典树 裸题 链表做法)
    G++ C++之区别
    PAT 乙级 1013. 数素数 (20)
    PAT 乙级 1012. 数字分类 (20)
    PAT 乙级 1009. 说反话 (20)
    PAT 乙级 1008. 数组元素循环右移问题 (20)
    HDU 6063 17多校3 RXD and math(暴力打表题)
    HDU 6066 17多校3 RXD's date(超水题)
  • 原文地址:https://www.cnblogs.com/Running-Time/p/4570000.html
Copyright © 2011-2022 走看看