给一字符串,每次操作把字符串中的两种字母交换,问最后交换完的字符串是多少
arr数组记录每个字母最后被替换成了哪个字母
读入字符前面加一空格 scanf(" %c %c", &a, &b);
#include <iostream> #include <cstdio> using namespace std; #define SZ 200005 char c[SZ]; int main() { int n, m; int arr[33]; scanf("%d %d", &n, &m); char a, b; scanf(" %s", c); int len = strlen(c); for(int i = 0; i < 26; i++) arr[i] = i; for(int i = 0; i < m; i++) { scanf(" %c %c", &a, &b); int aa = a - 'a', bb = b - 'a', p, q; for(int j = 0; j < 26; j++) if(arr[j] == aa) {p = j; break;} for(int j = 0; j < 26; j++) if(arr[j] == bb) {q = j; break;} arr[p] = bb; arr[q] = aa; } for(int j = 0; j < len; j++) { c[j] = arr[c[j] - 'a'] + 'a'; } printf("%s ", c); return 0; }