Ponk Warshall
链接:https://ac.nowcoder.com/acm/contest/7817/H
原字符串与新字符串的字符构成一对,那么如果A-A这种就不需要换,若形如A-B就和B-A交换,但如果没有B-A呢?
系统的归一下类:
A-A 0次
A-B B-A 1次
A-B B-C C-A 2次
A-B B-C C-D D-A 3次
for循环搞定,暴力,没做出来实在反思自己,太愚蠢了!。
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define mk make_pair const int maxn = 1e6+7; char s1[maxn]; char s2[maxn]; map<pair<char, char>, int > mp; char dir[4] = {'A','C','G','T'}; int main() { ll ans = 0; scanf("%s", s1); scanf("%s", s2); int len = strlen(s1); for(int i = 0; i < len; i++){ mp[mk(s1[i], s2[i])]++; } for(int i = 0; i < 4; i++){ for(int j = 0; j < 4; j++){ if(i == j){ continue; } int minn = min(mp[mk(dir[i], dir[j])], mp[mk(dir[j], dir[i])]); mp[mk(dir[j], dir[i])] -= minn; mp[mk(dir[i], dir[j])] -= minn; ans += minn; } } for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ if(i == j || j == k || i == k) continue; int x = min(min(mp[mk(dir[i], dir[j])], mp[mk(dir[j], dir[k])]), mp[mk(dir[k], dir[i])]); mp[mk(dir[i], dir[j])] -= x; mp[mk(dir[j], dir[k])] -= x; mp[mk(dir[k], dir[i])] -= x; ans += 2*x; } } } for(int i = 0; i < 4; i++) { for(int j = 0; j < 4; j++){ for(int k = 0; k < 4; k++){ for(int r = 0; r < 4; r++) { if(i == j || i == k || i == r || j == k || j == r || k == r) continue; int x = min(min(mp[mk(dir[i], dir[j])], mp[mk(dir[j], dir[k])]), min(mp[mk(dir[k], dir[r])], mp[mk(dir[r], dir[i])] )); mp[mk(dir[i], dir[j])] -= x; mp[mk(dir[j], dir[k])] -= x; mp[mk(dir[k], dir[r])] -= x; mp[mk(dir[r], dir[i])] -= x; ans += 3*x; } } } } cout << ans << endl; }