CodeForces 591 B. Rebranding 字符串处理

1. 题目描述

1.1. Limit

Time Limit: 2 seconds

Memory Limit: 256 megabytes

1.2. Problem Description

The name of one small but proud corporation consists of n n n lowercase English letters. The Corporation has decided to try rebranding — an active marketing strategy, that includes a set of measures to change either the brand (both for the company and the goods it produces) or its components: the name, the logo, the slogan. They decided to start with the name.

For this purpose the corporation has consecutively hired m m m designers. Once a company hires the i i i-th designer, he immediately contributes to the creation of a new corporation name as follows: he takes the newest version of the name and replaces all the letters x i xi xi by y i yi yi, and all the letters y i yi yi by x i xi xi. This results in the new version. It is possible that some of these letters do no occur in the string. It may also happen that x i xi xi coincides with y i yi yi. The version of the name received after the work of the last designer becomes the new name of the corporation.

Manager Arkady has recently got a job in this company, but is already soaked in the spirit of teamwork and is very worried about the success of the rebranding. Naturally, he can’t wait to find out what is the new name the Corporation will receive.

Satisfy Arkady’s curiosity and tell him the final version of the name.

1.3. Input

The first line of the input contains two integers n n n and m m m ( 1   ≤   n ,   m   ≤   200   000 1 \le n, m \le 200 000 1n,m200000) — the length of the initial name and the number of designers hired, respectively.

The second line consists of n n n lowercase English letters and represents the original name of the corporation.

Next m m m lines contain the descriptions of the designers’ actions: the i i i-th of them contains two space-separated lowercase English letters x i xi xi and y i yi yi.

1.4. Output

Print the new name of the corporation.

1.5. Sample Input 1

6 1

1.6. Sample Output 1

molice
police
p m

1.7. Sample Input 2

11 6
abacabadaba
a b
b c
a d
e g
f a
b b

1.8. Sample Output 2

cdcbcdcfcdc

1.9. Source

CodeForces 591 B. Rebranding


2. 解读

字符串处理,直接对整个输入字符串进行替换会超时,所以先构建一个26个字母组成的字符串,然后对字符串中的字母进行替换,最后对输入字符串进行替换。

3. 代码

#include <iostream>
#include <vector>
using namespace std;

vector<char> vt;

int main()
{
    int n, m, size;
    scanf("%d %d", &n, &m);
    string str;
    char a, b;
    cin >> str;
    size = 26;
    // vector存储
    for (int i = 0; i < size; i++) {
        vt.push_back('a' + i);
    }
    // 对26个字母进行替换
    for (int i = 0; i < m; i++) {
        cin >> a >> b;
        for (int j = 0; j < size; j++) {
            if (vt[j] == a) {
                vt[j] = b;
            } else if (vt[j] == b) {
                vt[j] = a;
            }
        }
    }
    // 对输入的字符串进行替换
    for (size_t i = 0; i < str.size(); i++) {
        str[i] = vt[str[i] - 'a'];
    }

    cout << str << endl;

    return 0;
}

联系邮箱:curren_wong@163.com

CSDNhttps://me.csdn.net/qq_41729780

知乎https://zhuanlan.zhihu.com/c_1225417532351741952

公众号复杂网络与机器学习

欢迎关注/转载,有问题欢迎通过邮箱交流。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值