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 ...
随机推荐
- Java中的mutable和immutable对象实例讲解
1.mutable(可变)和immutable(不可变)类型的区别 可变类型的对象:提供了可以改变其内部数据值的操作,其内部的值可以被重新更改. 不可变数据类型:其内部的操作不会改变内部的值,一旦试图 ...
- GDOI2018游记
前言 不知怎的,本蒟蒻居然拿到了GDOI参赛名额 于是乎,我稀里糊涂地跟着诸位大佬屁颠屁颠地来到了阔别已久的中山一中 腐败difficult and interesting的GDOI比赛就这样开始了. ...
- 最大两队竞争值(暴力dfs)--牛客多校第二场
题意: 给你2n个人,两两有对立竞争值,问你分成两队最大的竞争值是多少. 思路: 直接暴力dfs,稍微有点卡,3800ms. #include<iostream> #include< ...
- Uncaught SyntaxError: Unexpected identifier
$.ajax({ //请求头 type:"POST", contentType:"application/x-www-form-urlencoded", url ...
- 详解EveryThing
摘要:Everything几乎是每个职场人必备的效率工具,但同事们都只用它的一两个基本功能,并没有发挥出该软件的真正效率.实际上,把Everything的功能用到极致能够成倍的提升我们的工作效率,本文 ...
- html/form表单常用属性认识
1.form表单常用属性练习 <style> .form1 { margin: auto; height: 900px; width: 500px; text-align: center; ...
- 关于sqlmap的两个小坑
i春秋作家:__LSA__ 0x00 概述 近日在利用sqlmap注入测试时遇到一个奇怪的现象,高版本sqlmap无法检测出注入,但是低版本的可以测出注入,并且能跑出数据不是误报,经过对比测试和查看s ...
- iOS响应链和传递机制
iOS中加载的时候会先执行main函数 int main(int argc, charchar * argv[]) { @autoreleasepool { return UIApplicationM ...
- VMware安装CentOS6.3
这里测试的是 CentOS-6.3-x86_64-bin-DVD1 链接:点击进入提取码: zs32 如果这里CentOS下载太慢的话 点击进入下载
- Java定义栈结构,实现入栈、出栈操作
package com.example.demo; import java.util.ArrayList; public class Stack { ArrayList<Object> l ...