http://codeforces.com/contest/752/problem/B
uuu
yyu
xy
xx
注意变化了之后,检查一次前面已经变化过的就好。因为可能前面的满足,但是变了后不满足。

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> const int maxn = 1e3 + 20; struct node { char x, y; node(char xx, char yy) { if (xx > yy) { swap(xx, yy); } x = xx; y = yy; } bool operator < (const struct node & rhs) const { if (x != rhs.x) return x < rhs.x; else return y < rhs.y; } }; set<struct node>ans; char str[maxn]; char sub[maxn]; char book[maxn]; bool vis[maxn]; void work() { memset(book, '$', sizeof book); cin >> str + 1; cin >> sub + 1; for (int i = 1; sub[i]; ++i) book[sub[i]] = sub[i]; for (int i = 1; str[i]; ++i) { if (str[i] != book[sub[i]]) { char ch = book[sub[i]]; book[book[sub[i]]] = str[i]; book[str[i]] = ch; ans.insert(node(str[i], sub[i])); for (int j = 1; j <= i; ++j) { if (str[j] != book[sub[j]]) { // cout << i << endl; cout << -1 << endl; return; } } } } cout << ans.size() << endl; for (set<struct node> :: iterator it = ans.begin(); it != ans.end(); ++it) { cout << it->x << " " << it->y << endl; } } int main() { #ifdef local freopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endif work(); return 0; }