标签:link algo keyboard size class als while fine ace
题意:给定两个字符串a和b,问有多少种不同的字母组合对,使得将这些字母对替换字符串b后,可以变成字符串a。注意字母对彼此各不相同。
分析:vis[u]记录与u可形成关系的字母,若u与v不同,则形成字母对。若之后该关系被打破,则输出-1。
#pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define Min(a, b) ((a < b) ? a : b) #define Max(a, b) ((a < b) ? b : a) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1}; const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 1000 + 10; const int MAXT = 10000 + 10; using namespace std; char a[MAXN], b[MAXN]; const string s = "abcdefghijklmnopqrstuvwxyz"; map<char, int> mp; int vis[30]; int ans[30]; void init(){ for(int i = 0; i < 26; ++i){ mp[s[i]] = i + 1; } } int main(){ init(); while(scanf("%s%s", a, b) == 2){ memset(vis, 0, sizeof vis); memset(ans, 0, sizeof ans); int len = strlen(a); int cnt = 0; bool ok = true; for(int i = 0; i < len; ++i){ int tmpa = mp[a[i]], tmpb = mp[b[i]]; if(!vis[tmpa] && !vis[tmpb]){ vis[tmpa] = tmpb; vis[tmpb] = tmpa; if(tmpa != tmpb){ ans[cnt++] = tmpa; } } else if(vis[tmpa] != tmpb || vis[tmpb] != tmpa){ ok = false; break; } } if(!ok){ printf("-1\n"); } else{ printf("%d\n", cnt); for(int i = 0; i < cnt; ++i){ printf("%c %c\n", ans[i] + ‘a‘ - 1, vis[ans[i]] + ‘a‘ - 1); } } } return 0; }
CodeForces - 748B Santa Claus and Keyboard Check
标签:link algo keyboard size class als while fine ace
原文地址:http://www.cnblogs.com/tyty-Somnuspoppy/p/6486382.html