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 ...
随机推荐
- Type类的使用
Type类的使用(类反射)通过类获得Type: Type t = typeof(Person)通过实例对象获得类的Type: Type t = p.GetType()获取Type的方法:MethodI ...
- Smarty内置函数之config_load
config_load的作用是: 用于从配置文件中加载变量,属性file引入配置文件名,另外若配置文件包含多个部分,可以使用属性section指定从那部分取得变量(若不指定,将会引入失败). 实例: ...
- CVE-2018-19824漏洞学习
简介 在Linux内核4.19.6之前,本地用户可以通过在Sound / USB /card.c.的usb_audio_probe中错误处理一个恶意USB声音设备(没有接口)来利用ALSA驱动程序中的 ...
- redis 学习(9)-- redis 客户端 -- redis-py
redis 客户端 -- redis-py 简介 关于 redis 的各种客户端,我们可以在官网上寻找并使用,比如我这里的 python 客户端,可以在官网上找到:redis-client . 获取 ...
- oracle查看执行计划以及使用场景
文档结构: oracle执行计划使用场景 环境: Centos 6.10 Oracle 18.3.0.0.0 c 11g默认启动了自动统计信息收集的任务,默认运行时间是周一到周五晚上10点和周6,周天 ...
- ACM的一点基础知识
所摘内容来自于XJTU小学期ACM培训PPT log 默认以2为底 计算机一秒可以看作1e8次 保证数据计算精度及数据所需必要大小 a=1LL*a*a%p//在计算时通过乘以1LL,临时将Int转化为 ...
- 黑马java课程视频java学习视频
资料获取方式,关注公总号RaoRao1994,查看往期精彩-所有文章,即可获取资源下载链接 更多资源获取,请关注公总号RaoRao1994
- centso 7 Keepalived 配置脚本
#!/bin/bash #This is keepalived bashshell. #MASTER/BACKUP yum install -y openssl openssl-devel keepa ...
- 学习Linux让我进入了知名企业 原
说起我学习Linux的原因是多方面的,大学时我学的是物理学师范专业,有部分计算机课程,但我觉得这些课程没什么实际作用,我自己对计算机比较感兴趣,我利用业余时间学习了很多计算机技术.在大学期间我参加了很 ...
- 关于Linux单机、集群部署FastDFS分布式文件系统的步骤。
集群部署:2台tarcker服务器,2台storage服务器. 192.168.201.86 ---------(trackerd+storage+nginx) 192.168.201.87 ...