题意:给定两个字符串a和b,问有多少种不同的字母组合对,使得将这些字母对替换字符串b后,可以变成字符串a。注意字母对彼此各不相同。

分析:vis[u]记录与u可形成关系的字母,若u与v不同,则形成字母对。若之后该关系被打破,则输出-1。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
char a[MAXN], b[MAXN];
const string s = "abcdefghijklmnopqrstuvwxyz";
map<char, int> mp;
int vis[30];
int ans[30];
void init(){
for(int i = 0; i < 26; ++i){
mp[s[i]] = i + 1;
}
}
int main(){
init();
while(scanf("%s%s", a, b) == 2){
memset(vis, 0, sizeof vis);
memset(ans, 0, sizeof ans);
int len = strlen(a);
int cnt = 0;
bool ok = true;
for(int i = 0; i < len; ++i){
int tmpa = mp[a[i]], tmpb = mp[b[i]];
if(!vis[tmpa] && !vis[tmpb]){
vis[tmpa] = tmpb;
vis[tmpb] = tmpa;
if(tmpa != tmpb){
ans[cnt++] = tmpa;
}
}
else if(vis[tmpa] != tmpb || vis[tmpb] != tmpa){
ok = false;
break;
}
}
if(!ok){
printf("-1\n");
}
else{
printf("%d\n", cnt);
for(int i = 0; i < cnt; ++i){
printf("%c %c\n", ans[i] + 'a' - 1, vis[ans[i]] + 'a' - 1);
}
}
}
return 0;
}

  

CodeForces - 748B Santa Claus and Keyboard Check的更多相关文章

  1. Codeforces 784B Santa Claus and Keyboard Check

    题面: 传送门 B. Santa Claus and Keyboard Check Input file: standard input Output file: standard output Time ...

  2. Codeforces Round #389 Div.2 B. Santa Claus and Keyboard Check

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  3. B. Santa Claus and Keyboard Check 模拟

    http://codeforces.com/contest/752/problem/B uuu yyu xy xx 注意变化了之后,检查一次前面已经变化过的就好.因为可能前面的满足,但是变了后不满足. ...

  4. codeforces 748E Santa Claus and Tangerines

    E. Santa Claus and Tangerines time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. Codeforces 752C - Santa Claus and Robot - [简单思维题]

    题目链接:http://codeforces.com/problemset/problem/752/C time limit per test 2 seconds memory limit per t ...

  6. Codeforces 748D Santa Claus and a Palindrome

    雅礼集训期间我好像考完试就开始划水了啊 给出k个长度相同的字符串,每个串有一个权值,选出一些串连成一个回文串.使得选中的串的总权值最大. 如果选一个串,必须同时选一个对称的串.还有一个特殊情况是可以在 ...

  7. CodeForces 748C Santa Claus and Robot (思维)

    题意:给定一个机器人的行走路线,求最少的点能使得机器人可以走这样的路线. 析:每次行走,记录一个方向向量,每次只有是相反方向时,才会增加一个点,最后再加上最后一个点即可. 代码如下: #pragma ...

  8. CodeForces - 748E Santa Claus and Tangerines(二分)

    题意:将n个蛋糕分给k个人,要保证每个人都有蛋糕或蛋糕块,蛋糕可切, 1.若蛋糕值为偶数,那一次可切成对等的两块. 2.若蛋糕值为奇数,则切成的两块蛋糕其中一个比另一个蛋糕值多1. 3.若蛋糕值为1, ...

  9. CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)

    题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的 ...

随机推荐

  1. An attempt was made to call the method com.google.gson.GsonBuilder.setLenient()Lcom/google/gson/GsonBuilder; but it does not exist. Its class, com.google.gson.GsonBuilder, is available from the foll

    SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/G:/sharp/repo ...

  2. oracle根据一张表更新另外一张表

    知道是两张表进行更新,之前作过mysql的,直接就写了: update a,b set a.code = b.code wehre a.id = b.id 然后就报错了,上网查了下知道oracle不能 ...

  3. uboot如何启动内核

    2.7.1.uboot和内核到底是什么 2.7.1.1.uboot是一个裸机程序 (1)uboot的本质就是一个复杂点的裸机程序.和我们在ARM裸机全集中学习的每一个裸机程序并没有本质区别. 2.7. ...

  4. Day7 - B - Super A^B mod C FZU - 1759

    Given A,B,C, You should quickly calculate the result of A^B mod C. (1<=A,C<=1000000000,1<=B ...

  5. 005.CI4框架CodeIgniter, 通过带路径的view视图访问

    01. 我们在app目录的Views文件夹中新建一个Login文件,再创建一个login.php文件,在里面我们写上如下代码: <!doctype html> <html> & ...

  6. 010.Oracle数据库 , ORDER BY 按升序降序排序

    /*Oracle数据库查询日期在两者之间*/ SELECT DISTINCT ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date( '2017-0 ...

  7. DOM基础1

    Document Object Model 文档对象模型 1.改内容: innerHTML 例:div1.innerHTML = "我能干<br />什么";      ...

  8. spark源码单步跟踪阅读-从毛片说起

    想当年读大学时,那时毛片还叫毛片,现在有文明的叫法了,叫小电影或者爱情动作片.那时宿舍有位大神,喜欢看各种毛片,当我们纠结于毛片上的马赛克时,大神大手一挥说道:这算啥,阅尽天下毛片,心中自然无码!突然 ...

  9. C++实现单链表的12种基本操作

    C++单链表的操作2017-12-25 1 // 单链表.cpp: 定义控制台应用程序的入口点. //Author:kgvito //Date: 2017.12.25 #include "s ...

  10. [LeetCode] 934. Shortest Bridge 最短的桥梁

    In a given 2D binary array A, there are two islands.  (An island is a 4-directionally connected grou ...