codeforce 748B map 应用
来源:互联网 发布:大华电子秤数据下传 编辑:程序博客网 时间:2024/06/07 16:06
题目:
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
题意:
给出两个字符串,找出字母替换的方案,如果无需替换则输出0,无法替换则输出-1,能替换则输出替换方案
#include <bits/stdc++.h> using namespace std; int main() { int w=0; string s1,s2; map<char,char > m; cin>>s1>>s2; for(int i=0;i<s1.size();i++) { if(m[s1[i]]!=s2[i] && m[s1[i]]!=0) { cout<<-1<<endl; return 0; } if(m[s2[i]]!=s1[i] && m[s2[i]]!=0) { cout<<-1<<endl; return 0; } m[s1[i]]=s2[i]; m[s2[i]]=s1[i]; } for(char i='a';i<='z';i++) { if(m[i]!=0 && m[i]!=i) { w++; m[m[i]]=0; } } cout<<w<<endl; for(char i='a';i<='z';i++) { if(m[i]!=0 && m[i]!=i) { cout<<i<<' '<<m[i]<<endl; } } return 0; }
- codeforce 748B map 应用
- codeforce 748B
- codeforce-501【B思维&&map】【C思维&&树】
- codeforce 12C map
- map codeforce Chat Order
- Codeforce # B Sequence Formatting
- codeforce 298 B Sail
- 【codeforce】B. Flag Day
- codeforce B. Road Construction
- CodeForce 126B
- codeforce #247 B(div2)
- codeforce 6B dfs
- codeforce 7B
- codeforce 9B
- codeforce 13B
- codeforce 15B
- codeforce 17 B
- codeforce 18B
- c++简单24点游戏
- 第二讲 Groovy语言基础
- 加斯帕《数目的游戏问题》
- Android开发艺术探索学习摘要(二)
- 关于跨域
- codeforce 748B map 应用
- RESTful服务最佳实践——(十二)
- i++是否原子操作?并解释为什么?
- poj 2352 Stars(树状数组)
- Go语言学习之net/http包(The way to go)
- 如何理解和应用闭包
- DAC0832等效为可变电阻
- 递归递推问题 H 三国佚事——巴蜀之危
- Could not get BatchedBridge, make sure your bundle is packaged correctly