题目链接

问题分析

由于三个字母是等价的,所以大致可以分为如下几种情况:

  • aa, ab

  • ab, ac

  • ab, ba

  • ab, bc

不难发现,第\(3\)中情况可能造成无解(\(n>1\)时),而剩下的情况都可以由\(aaabbbccc\)或\(abcabcabc\)这样的串解决。所以直接枚举\(3\)个字母的全排列,然后拓展成上面两种情况分别判断一下即可。

参考程序

#include <bits/stdc++.h>
using namespace std; int n;
char A[ 10 ], B[ 10 ];
int Map[ 10 ][ 10 ];
int Ans[ 10 ]; bool Check1() {
return Map[ Ans[ 1 ] ][ Ans[ 2 ] ] || Map[ Ans[ 2 ] ][ Ans[ 3 ] ] || Map[ 1 ][ 1 ] || Map[ 2 ][ 2 ]|| Map[ 3 ][ 3 ];
} bool Check2() {
return Map[ Ans[ 1 ] ][ Ans[ 2 ] ] || Map[ Ans[ 2 ] ][ Ans[ 3 ] ] || Map[ Ans[ 3 ] ][ Ans[ 1 ] ];
} int main() {
scanf( "%d", &n );
scanf( "%s%s", A + 1, B + 1 );
Map[ A[ 1 ] - 'a' + 1 ][ A[ 2 ] - 'a' + 1 ] = 1;
Map[ B[ 1 ] - 'a' + 1 ][ B[ 2 ] - 'a' + 1 ] = 1;
Ans[ 1 ] = 1; Ans[ 2 ] = 2; Ans[ 3 ] = 3;
if( n == 1 ) {
while( Map[ Ans[ 1 ] ][ Ans[ 2 ] ] || Map[ Ans[ 2 ] ][ Ans[ 3 ] ] )
if( !next_permutation( Ans + 1, Ans + 4 ) ) {
printf( "NO\n" );
return 0;
}
printf( "YES\n" );
printf( "%c%c%c\n", Ans[ 1 ] + 'a' - 1, Ans[ 2 ] + 'a' - 1, Ans[ 3 ] + 'a' - 1 );
return 0;
}
while( true ) {
if( !Check1() ) {
printf( "YES\n" );
for( int i = 1; i <= n; ++i ) printf( "%c", Ans[ 1 ] + 'a' - 1 );
for( int i = 1; i <= n; ++i ) printf( "%c", Ans[ 2 ] + 'a' - 1 );
for( int i = 1; i <= n; ++i ) printf( "%c", Ans[ 3 ] + 'a' - 1 );
printf( "\n" );
return 0;
}
if( !Check2() ) {
printf( "YES\n" );
for( int i = 1; i <= n; ++i )
printf( "%c%c%c", Ans[ 1 ] + 'a' - 1, Ans[ 2 ] + 'a' - 1, Ans[ 3 ] + 'a' - 1 ) ;
printf( "\n" );
return 0;
}
if( !next_permutation( Ans + 1, Ans + 4 ) ) {
printf( "NO\n" );
return 0;
}
}
return 0;
}

CF1213E Two Small Strings的更多相关文章

  1. Hacker Rank: Two Strings - thinking in C# 15+ ways

    March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...

  2. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  3. Multiply Strings

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  4. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  5. [LeetCode] Encode and Decode Strings 加码解码字符串

    Design an algorithm to encode a list of strings to a string. The encoded string is then sent over th ...

  6. [LeetCode] Group Shifted Strings 群组偏移字符串

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  7. [LeetCode] Isomorphic Strings 同构字符串

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  8. [LeetCode] Multiply Strings 字符串相乘

    Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...

  9. 使用strings查看二进制文件中的字符串

    使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...

随机推荐

  1. .Net Core 中使用NLog作为日志中间件

    ⒈安装相关依赖 NLog NLog.Web.AspNetCore ⒉在项目的根目录中创建NLog配置文件 <?xml version="1.0" encoding=" ...

  2. C++练习 | 单链表的创建与输出(结构体格式)

    #include <iostream> #include <stdio.h> using namespace std; #define OK 1 #define ERROR 0 ...

  3. GitHub从小白到熟悉<二>

    创建 仓库  

  4. python网络编程-socket套接字通信循环-粘包问题-struct模块-02

    前置知识 不同计算机程序之间数据的传输 应用程序中的数据都是从程序所在计算机内存中读取的. 内存中的数据是从硬盘读取或者网络传输过来的 不同计算机程序数据传输需要经过七层协议物理连接介质才能到达目标程 ...

  5. asp.net core在发布时排除配置文件

    使用命令发布 dotnet restore dotnet publish -c Release -r win-x64 -o "D:\services" 这样发布总是是将配置文件覆盖 ...

  6. Docker:Swarm + Stack 一站式部署容器集群

    参考1 参考2 1.注意docker的版本,yum默认安装的版本比较低,可能出现 unsupported Compose file version: 3.7 docker版本升级 2.docker-c ...

  7. 首个springboot项目总结

    项目说明:采用分布式架构的基于restfulApi的后端微服务api项目. 用到的技术栈:springboot.mybatis.swagger.eureka注册中心.gateway.awss3.doc ...

  8. pt-archiver配置自动归档

    Mysql的数据归档通常使用percona的pt-archiver.通过shell脚本加crontab可以应对大多数场景下的数据自动归档. 安装 Percona Toolkit的安装不再赘述,请自行搜 ...

  9. 17、Nginx HTTPS 实践

    1.HTTPS安全证书基本概述 为什么需要使用HTTPS, 因为HTTP不安全.当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡 ...

  10. ctfd搭建

    CTFd 0x00 前言 搭个CTF平台,看能不能带动一下学校的CTF参与度. 一个下午都在搭这个平台:O 抓瞎摸索,最后成功用Apache+mod_wsgi也算是功德圆满了. 进入正题: 系统: C ...