题目链接

问题分析

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

  • 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. 什么是PWA

    什么是PWA:https://www.jianshu.com/p/299c9c720e56 2019前端必会黑科技之PWA:https://www.jianshu.com/p/098af61bbe04 ...

  2. 第五篇 jQuery特效与动画

    5.1 show()与hide()方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...

  3. flume复习(二)

    一.简介:flume是一种分布式.可靠且可用的系统,能够用于有效的从不同的源收集.聚合和移动大量的日志数据到集中式数据存储.它具有基于流数据的简单灵活的架构,它具有健壮的可靠性机制和许多故障转移和恢复 ...

  4. hashmap的hash方法源doc解读

    /** * Computes key.hashCode() and spreads (XORs) higher bits of hash * to lower. Because the table u ...

  5. IE浏览器清除缓存及历史浏览数据

    IE浏览器清除缓存方法如下: 打开IE浏览器,依次点击"工具-Internet选项-常规-删除",如下图所示, 有的时候发现你明明已经执行了删除,但是实际上还是有缓存数据,一般是因 ...

  6. Ubuntu中用bitbake core-image-minimal时,出错:from bb import data

    问题描述: 在准备ARM交叉编译环境时,执行完命令: DISTRO=fsl-imx-x11 MACHINE=imx6qsabresd source fsl-setup-release.sh -b bu ...

  7. linux 查看cpu核心数

    1.查看CPU个数 cat /proc/cpuinfo |grep "physical id"|sort|uniq|wc -l 2.查看每个物理CPU含有的核心个数 cat /pr ...

  8. SAP中MM模块基础数据之Quota Arrangement(配额协议)的解析

    有的时候我们的采购部门有这样的需求, 同一颗物料有几个供应商同时供料, 这个时候就涉及到一个问题, 避免出现总是和一家供应商购买物料的情况,我们需求把这些物料按照一定的比列分配给供应商.在SAP系统中 ...

  9. AVAYA_Site_administrator软件简单操作

    AVAYA_Site_administrator软件简单操作 1.配置软件(第一次登录) 点击File>New>Voice System  在弹出对话框随意输入一个名称 下一步,默认选择 ...

  10. string::find_first_not_of

    string (1) size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept; c-string ...