文章目錄
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
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
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;
}
聯繫郵箱:[email protected]
CSDN:https://me.csdn.net/qq_41729780
知乎:https://zhuanlan.zhihu.com/c_1225417532351741952
公衆號:複雜網絡與機器學習
歡迎關注/轉載,有問題歡迎通過郵箱交流。