Codeforces - 748B Santa Claus and Keyboard Check(字符串水题)
2017-04-06 22:44
411 查看
点击打开题目链接
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
output
input
output
input
output
题目大意:有一个键盘有些键顺序反了,给出两组字符串,找出安反的字符对。
只需要加一个int类的vis数组来保存两个字符的相对关系即可。一开始一直卡第14个样例WA,最
e214
后字符转数组都在最后加了一个1就AC了.
附上AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
string s,st;
int vis[maxn],ss[maxn];
int num=0,flag=1;
int main()
{
cin>>s>>st;
for(int i=0;i<s.length();i++)
{
if(!vis[s[i]-'a'+1]&&!vis[st[i]-'a'+1])
{
vis[s[i]-'a'+1]=st[i]-'a'+1;
vis[st[i]-'a'+1]=s[i]-'a'+1;
if(s[i]!=st[i])ss[num++]=s[i]-'a'+1;
}
else if(vis[s[i]-'a'+1]!=st[i]-'a'+1||vis[st[i]-'a'+1]!=s[i]-'a'+1)
{
flag=0;break;
}
}
if(!flag)cout<<-1<<endl;
else {
cout<<num<<endl;
for(int i=0;i<num;i++)
printf("%c %c\n",ss[i]+'a'-1,vis[ss[i]]+'a'-1);
}
return 0;
}
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
helloworld ehoolwlroz
output
3 h e l o d z
input
hastalavistababy hastalavistababy
output
0
input
merrychristmas christmasmerry
output
-1
题目大意:有一个键盘有些键顺序反了,给出两组字符串,找出安反的字符对。
只需要加一个int类的vis数组来保存两个字符的相对关系即可。一开始一直卡第14个样例WA,最
e214
后字符转数组都在最后加了一个1就AC了.
附上AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1000+5;
string s,st;
int vis[maxn],ss[maxn];
int num=0,flag=1;
int main()
{
cin>>s>>st;
for(int i=0;i<s.length();i++)
{
if(!vis[s[i]-'a'+1]&&!vis[st[i]-'a'+1])
{
vis[s[i]-'a'+1]=st[i]-'a'+1;
vis[st[i]-'a'+1]=s[i]-'a'+1;
if(s[i]!=st[i])ss[num++]=s[i]-'a'+1;
}
else if(vis[s[i]-'a'+1]!=st[i]-'a'+1||vis[st[i]-'a'+1]!=s[i]-'a'+1)
{
flag=0;break;
}
}
if(!flag)cout<<-1<<endl;
else {
cout<<num<<endl;
for(int i=0;i<num;i++)
printf("%c %c\n",ss[i]+'a'-1,vis[ss[i]]+'a'-1);
}
return 0;
}
相关文章推荐
- codeforces 96A (字符串模拟水题)
- codeforces 133A HQ9+(字符串水题)
- codeforces 118A String Task(字符串水题)
- codeforces 716B字符串水题
- codeforces 112APetya and Strings(字符串水题)
- codeforces 236A . Boy or Girl(字符串水题)
- codeforces 112APetya and Strings(字符串水题)
- hdu 1039 (字符串水题)
- CodeForces 534A-A - Exam-构造水题
- hdu 1048 字符串水题
- Codeforces 462A Appleman and Easy Task(水题)
- LeetCode | 344. Reverse String 字符串水题
- HDU 1228 A + B 字符串水题
- 古代密码 字符串水题
- CodeForces 589A -- A. Email Aliases (字符串水题 STL)
- CodeForces 483B(水题)
- Codeforces 596A Wilbur and Swimming Pool 【水题】
- CodeForces 688B Lovely Palindromes (水题回文)
- Codeforces 385B Bear and Strings(字符串)
- Codeforces 382A Ksenia and Pan Scales(水题)