Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
2016-12-25 22:51
465 查看
B. Santa Claus and Keyboard Check
原题链接:http://codeforces.com/problemset/problem/748/B
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
题意:
通俗地讲,就是问有几组对应关系,并输出。例如a和b对应,就是一组对应关系,但是如果是a和a本身对应,那么不应该计入对应关系,但是也是对应。特别的,如果是a和b对应,然后a(或者b)又和c对应,那么这种对应关系不成立,则输出-1
给一组例子:ab
aa
答案应该输出-1, a先和a对应,然后a又和b对应,说明对应关系出错了,所以……
代码:
原题链接:http://codeforces.com/problemset/problem/748/B
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
题意:
通俗地讲,就是问有几组对应关系,并输出。例如a和b对应,就是一组对应关系,但是如果是a和a本身对应,那么不应该计入对应关系,但是也是对应。特别的,如果是a和b对应,然后a(或者b)又和c对应,那么这种对应关系不成立,则输出-1
给一组例子:ab
aa
答案应该输出-1, a先和a对应,然后a又和b对应,说明对应关系出错了,所以……
代码:
#include<stdio.h> #include<string.h> #include<map> using namespace std; struct node { char x,y; } q[100];//存储符合条件的对应关系 char a[1010],b[1010]; int main() { scanf("%s %s",a,b); int i,flag=0,ps=0,j; int len=strlen(a); map<char,bool>vist;//记录自己对应自己的对应关系 for(i=0; i<len; i++) { if(a[i]==b[i]) { for(j=0; j<ps; j++) { if(a[i]==q[j].x||a[i]==q[j].y)//查询之前是否出现过对应关系 { flag=1; break; } } if(flag) break; else { vist[a[i]]=true;//记录为本身的对应关系 continue; } } else { int flog=0; for(j=0; j<ps; j++) { if((q[j].x==a[i]&&q[j].y==b[i])||(q[j].x==b[i]&&q[j].y==a[i]))//去重 { flog=1; break; } else if((q[j].x==a[i]&&q[j].y!=b[i])||(q[j].y==a[i]&&q[j].x!=b[i])|(q[j].x==b[i]&&q[j].y!=a[i])||(q[j].y==b[i]&&q[j].x!=a[i]))//判断是否为错误的对应关系 { flag=1; break; } } if(vist[a[i]]||vist[b[i]])//判断是否出现过对应自身的对应关系 { flag=1; break; } if(flag) break; if(flog) continue; q[ps].x=a[i]; q[ps].y=b[i]; ps++; } } if(flag) printf("-1\n"); else { printf("%d\n",ps); for(i=0; i<ps; i++) printf("%c %c\n",q[i].x,q[i].y); } return 0; }
相关文章推荐
- 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(模拟)
- Codeforces Round 389 Div.2 B. Santa Claus and Keyboard Check(模拟)
- Codeforces Round #389(Div.2)C Santa Claus and Robot【思维】
- B - Santa Claus and Keyboard Check CodeForces - 748B
- Codeforces Round #389 (Div. 2)B. Santa Claus and Keyboard Check(模拟)
- 模拟· CodeForces - 748B · Santa Claus and Keyboard Check
- B. Santa Claus and Keyboard Check 模拟
- Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check
- B - Santa Claus and Keyboard Check(模拟)