CodeForces - 748B Santa Claus and Keyboard Check

题意:给定两个字符串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;
}

  

时间: 2024-10-13 10:19:43

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

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

题意:给定k个长度为n的字符串,每个字符串有一个魅力值ai,在k个字符串中选取字符串组成回文串,使得组成的回文串魅力值最大. 分析: 1.若某字符串不是回文串a,但有与之对称的串b,将串a和串b所有的魅力值分别从大到小排序后,若两者之和大于0,则可以放在回文串的两边. 2.若某字符串是回文串,将其魅力值从大到小排序后,两两依次分析:(mid---可能放在回文串中间的串的最大魅力值) (1)若两个数都是正的,那么就将其放在两边,并将结果计入ans.(ans---回文串两边的串的魅力值之和) (2)

[2015-12-24]OMG美语每日笔记-Santa Claus

坚持学习英语,OMG口语非常长不错,坚持每天整理.学英语坚持最重要,学英语坚持最重要,学英语坚持最重要说三遍! Santa Claus is coming to town 圣诞老人进城来! There are so many Christmas songs about Santa .Santa Claus is coming to town.Is a classic example. 关于圣诞老人的歌很多,圣诞老人进城来,是一个比较经典的歌. Santa Baby 圣诞宝贝 I love lis

291 - The House Of Santa Claus

来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=4&problem=227&mosmsg=Submission+received+with+ID+14026069 The House Of Santa Claus In your childhood you most likely had to solve the ri

【Codeforces748D】Santa Claus and a Palindrome [STL]

Santa Claus and a Palindrome Time Limit: 20 Sec  Memory Limit: 512 MB Description 有k个串,串长都是n,每个串有一个ai的贡献. 选出若干个串,若它们可以通过任意组合,形成一个回文串,则可以获得它们的贡献之和. 求最大贡献. Input 第一行两个整数k,n. 之后k行,每行分别是一个串si,与贡献ai. Output 一个整数表示答案. Sample Input 7 3 abb 2 aaa -3 bba -1 z

UVA 291 The House Of Santa Claus DFS

题目: In your childhood you most likely had to solve the riddle of the house of Santa Claus. Do you remember that the importance was on drawing the house in a stretch without lifting the pencil and not drawing a line twice? As a reminder it has to look

cf 478D.Santa Claus and a Palindrome

原来set,priority_queue也可以映射..涨姿势2333 比较麻烦的应该就是判断自身回文的串是选2个还是选一个吧. 1 #include<bits/stdc++.h> 2 #define INF 0x7fffffff 3 #define LL long long 4 #define N 100005 5 #define pill pair<int ,int > 6 using namespace std; 7 inline int ra() 8 { 9 int x=0,

UVa 291 The House Of Santa Claus 回溯dfs

题意:从左下方的1开始,一笔画出圣诞老人的房子. 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 int edge[6][6]; 5 6 //已画了k条边 准备将端点x加入进来 7 void dfs(int x,int k,string s){ 8 s+=char(x+'0'); 9 if(k==8){ 10 cout<<s<<endl; 11 return; 12 }

Codeforces 1165F2(二分内的check)

要点 二分答案,内部喜闻乐见的拖延策略:对于某个打折玩具,就选最晚的打折时间买,答案并不会变劣,只是购买时间的平移. 注意最晚时间不是预处理的东西,是二分内部的.在mid以内的最晚时间. #include <cstdio> #include <algorithm> #include <vector> using namespace std; const int maxn = 2e5 + 5; int n, m, k[maxn], d[maxn], t[maxn]; in

Educational Codeforces Round 79 (Rated for Div. 2) D. Santa&#39;s Bot

链接: https://codeforces.com/contest/1279/problem/D 题意: Santa Claus has received letters from n different kids throughout this year. Of course, each kid wants to get some presents from Santa: in particular, the i-th kid asked Santa to give them one of