Codeforces 748B-Santa Claus and Keyboard Check
2016-12-28 21:50
288 查看
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
output
input
output
input
output
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
helloworld ehoolwlroz
output
3 h e l o d z
input
hastalavistababy hastalavistababy
output
0
input
merrychristmas christmasmerry
output
-1
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <vector> #include <set> using namespace std; #define LL long long int main() { char ch1[1005],ch2[1005]; int visit[30],a[30]; while(~scanf("%s",ch1)) { scanf("%s",ch2); memset(visit,0,sizeof visit); int len=strlen(ch1),flag=1,sum=0; for(int i=0;i<len;i++) { if(!visit[ch1[i]-'a'+1]&&!visit[ch2[i]-'a'+1]) { visit[ch1[i]-'a'+1]=ch2[i]-'a'+1; visit[ch2[i]-'a'+1]=ch1[i]-'a'+1; if(ch1[i]!=ch2[i]) a[++sum]=ch1[i]-'a'+1; } else if(ch2[i]-'a'+1!=visit[ch1[i]-'a'+1]||ch1[i]-'a'+1!=visit[ch2[i]-'a'+1]) flag=0; } if(!flag) {printf("-1\n");continue;} printf("%d\n",sum); for(int i=1;i<=sum;i++) printf("%c %c\n",a[i]+'a'-1,visit[a[i]]+'a'-1); } return 0; }
相关文章推荐
- 模拟· CodeForces - 748B · Santa Claus and Keyboard Check
- 【Codeforces 748 B Santa Claus and Keyboard Check】
- 【codeforces 752B】Santa Claus and Keyboard Check
- B - Santa Claus and Keyboard Check CodeForces - 748B
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Santa Claus and Keyboard Check (字符串)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- B. Santa Claus and Keyboard Check 模拟
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #389 (Div. 2)B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)