题意
给出由小写字母初始字符串,每次操作将字符串中所有为 \(c\) 的字符改为 \(d\)。输出最终的字符串。
分析
很明显只需要开一个 \(fa\) 数组,其中 \(fa[i]=j\) 表示字母 \(i\) 被改为了 \(j\)。对于每次操作只需要遍历 \(26\) 个字母,将 \(fa[i]=c\) 的那些字母改成 \(fa[i]=d\)。时间复杂度 \(O(wq),w=26\)。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n, q, fa[30];
string s;
inline int read(int &x) {
char ch = x = 0;
int m = 1;
while (ch < '0' || ch > '9') {
ch = getchar();
if (ch == '-') m *= -1;
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + ch - 48;
ch = getchar();
}
x *= m;
return x;
}
inline void print(int x) {
if (x < 0) putchar('-'), x = -x;
static int stk[50];
int top = 0;
do {
stk[top++] = x % 10;
x /= 10;
} while (x);
while (top) {
putchar(stk[--top] + 48);
}
putchar('\n');
return ;
}
signed main() {
for (int i = 0; i < 26; i++) fa[i] = i;
read(n);
cin >> s;
read(q);
char c, d;
while (q--) {
c = d = 0;
while (c < 'a' || c > 'z') c = getchar();
while (d < 'a' || d > 'z') d = getchar();
for (int i = 0; i < 26; i++) {
if (fa[i] == (int)(c - 'a')) {
fa[i] = (int)(d - 'a');
}
}
}
for (int i = 0; i < s.size(); i++) {
putchar(fa[s[i] - 'a'] + 'a');
}
return 0;
}
标签:26,ch,int,题解,fa,while,Many,ABC342C,getchar
From: https://www.cnblogs.com/iloveoi/p/18037754