C - Many Replacement
https://atcoder.jp/contests/abc342/tasks/abc342_c
思路
根据q组字符转换动作,找出每个字符最终将变成的字符。
初始化字母转换表:
映射前 -> 映射后
a -> a
b -> b
......z -> z
对于q组字符变换, 依次执行:
c -> d
如果 c 在 “映射后” 字符集合中存在, 则将“字母转换表中”的“映射后”的值替换为 d,
否则,不做替换。
Code
https://atcoder.jp/contests/abc342/submissions/50626633
int n; string s; int q; int main() { cin >> n; cin >> s; map<char, char> cc; for(char one='a'; one<='z'; one++){ cc[one] = one; } cin >> q; for(int i=0; i<q; i++){ char c, d; cin >> c >> d; if (c == d) { continue; } for(auto it: cc){ char oldc = it.first; char newc = it.second; // cout << "oldc=" << oldc << " newc="<< newc << endl; if (newc == c){ cc[oldc] = d; // cout << "oldc=" << oldc << " cc[oldc]="<< cc[oldc] << endl; } } // cc[c] = d; } for(int i=0; i<s.size(); i++){ char one = s[i]; if (cc[one]){ s[i] = cc[one]; } } cout << s << endl; return 0; }
标签:字符,映射,int,Many,char,abc342,Replacement From: https://www.cnblogs.com/lightsong/p/18032216