CF1213E Two Small Strings
问题分析
由于三个字母是等价的,所以大致可以分为如下几种情况:
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的更多相关文章
- Hacker Rank: Two Strings - thinking in C# 15+ ways
March 18, 2016 Problem statement: https://www.hackerrank.com/challenges/two-strings/submissions/code ...
- 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 ...
- Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- [LeetCode] Add Strings 字符串相加
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...
- [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 ...
- [LeetCode] Group Shifted Strings 群组偏移字符串
Given a string, we can "shift" each of its letter to its successive letter, for example: & ...
- [LeetCode] Isomorphic Strings 同构字符串
Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...
- [LeetCode] Multiply Strings 字符串相乘
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
- 使用strings查看二进制文件中的字符串
使用strings查看二进制文件中的字符串 今天介绍的这个小工具叫做strings,它实现功能很简单,就是找出文件内容中的可打印字符串.所谓可打印字符串的涵义是,它的组成部分都是可打印字符,并且以nu ...
随机推荐
- Go语言中 Print,Println 和 Printf 的区别(八)
Print 和 Println 这两个打印方式类似,只在格式上有区别 1. Println 打印的每一项之间都会有空行,Print 没有,例如: fmt.Println("go", ...
- # log对数Hash映射优化
log对数Hash映射优化 利用了一个数学技巧:$\forall k \in [0,35],2^{k} mod 37 互不相等,且恰好取遍整数1-36 $ 应用:将int范围内的\(2^k映射到k\) ...
- C++练习 | 文件流应用(1)
#include <iostream> #include <cmath> #include <cstring> #include <string> #i ...
- MySQL中的SQL的常见优化策略
MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 1 避免全表扫描对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索 ...
- Windows 2008 R2阿里云安全基线检查
设置密码使用期限策略在管理工具打开本地安全策略,打开路径:安全设置\帐户策略\密码策略,将密码最长使用期限设置为30-180之间,建议值为90,将密码最短使用期限设置为1-14之间,建议值为7. 风险 ...
- C++ 多态、虚函数(virtual 关键字)、静态联编、动态联编
函数重写:(在子类中重写父类中的函数) 父类中被重写的函数 依然会继承 给子类. 子类中重写的函数将覆盖父类中的函数. 通过作用域分辨符 :: 可以访问到父类中的函数. 例如: #includ ...
- Action注入错误
2016-07-13 13:52:09,584 [ERROR]-[com.opensymphony.xwork2.util.logging.commons.CommonsLogger:38] Exce ...
- 彻底了解websocket原理
一.websocket与http WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环连接的不算) 首先HTTP有 1 ...
- 小程序之textarea层级最高问题
1.textarea位于底部固定定位按钮下方,会导致点击底部按钮,textarea获取到焦点. 解决方法如下 view与textarea之间在聚焦和失去焦点进行切换 cursor-spacing是te ...
- pgsql sql字段拼接
1. 一条记录数据字段拼接 语法:concat_ws('拼接符号',字段名,more fields) 例子:concat_ws(':',username,sex)2. 多条记录字段拼接 语法:con ...