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 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 designers. Once a company hires the -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 by , and all the letters by . 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 coincides with . 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 and () — the length of the initial name and the number of designers hired, respectively.
The second line consists of lowercase English letters and represents the original name of the corporation.
Next lines contain the descriptions of the designers' actions: the -th of them contains two space-separated lowercase English letters and .
1.4. Output#
Print the new name of the corporation.
1.5. Sample Input 1#
6 1
police
p m
1.6. Sample Output 1#
molice
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#
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
CSDN:https://me.csdn.net/qq_41729780
知乎:https://zhuanlan.zhihu.com/c_1225417532351741952
公众号:复杂网络与机器学习
欢迎关注/转载,有问题欢迎通过邮箱交流。