CF

来源:互联网 发布:淘宝950轮播图片 编辑:程序博客网 时间:2024/05/02 02:02

1.题目描述:

B. Santa Claus and Keyboard Check
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another key, which is located exactly where the first key should be.

In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard.

You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once.

Input

The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters.

Output

If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print «-1» (without quotes).

Otherwise, the first line of output should contain the only integer k (k ≥ 0) — the number of pairs of keys that should be swapped. The following k lines should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct.

If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair.

Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes.

Examples
input
helloworldehoolwlroz
output
3h el od z
input
hastalavistababyhastalavistababy
output
0
input
merrychristmaschristmasmerry
output
-1


2.题意概述:

给定一个原串和一个新串,它们之中存在一些序偶,(x, y) 在原串的i位置为x,在新串的i位置是y,且x != y 则存在序偶(x, y),原串的所有x在新串中必须只用y表示。一个字母最多出现在一个序偶中,问这样的序偶有多少对。

3.解题思路:

先贪心地扫一遍确定序偶,再扫一遍判断是否矛盾

4.AC代码:

#include <iostream>#include <cstdio>#include <string>#include <map>#define maxn 101000using namespace std;string p, s;map<char, char> mp, mp1;map<char, int> flag;int main(){cin >> p >> s;int sz = p.size();int ans = 1;for (int i = 0; i < sz; i++){if (p[i] != s[i]){if (flag[p[i]]){if (mp1[p[i]] != s[i]){ans = 0;break;}}else if (flag[s[i]]){ans = 0;break;}else{mp[p[i]] = s[i];flag[p[i]] = 1;flag[s[i]] = 1;mp1[p[i]] = s[i];mp1[s[i]] = p[i];}}}for (int i = 0; i < sz; i++){if (s[i] == p[i]){if (flag[s[i]]){ans = 0;break;}}}if (ans){printf("%d\n", (int)mp.size());for (auto i = mp.begin(); i != mp.end(); i++)printf("%c %c\n", i->first, i->second);}elseputs("-1");return 0;}

0 0
原创粉丝点击