AtCoder Grand Contest #026 C - String Coloring
Time Limit: 3 sec / Memory Limit: 1024 MB
Score : 600600 points
Problem Statement
You are given a string SS of length 2N2N consisting of lowercase English letters.
There are 22N22N ways to color each character in SS red or blue. Among these ways, how many satisfy the following condition?
- The string obtained by reading the characters painted red from left to right is equal to the string obtained by reading the characters painted blue from right to left.
Constraints
- 1≤N≤181≤N≤18
- The length of SS is 2N2N.
- SS consists of lowercase English letters.
Input
Input is given from Standard Input in the following format:
NN
SS
Output
Print the number of ways to paint the string that satisfy the condition.
Sample Input 1 Copy
4
cabaacba
Sample Output 1 Copy
4
There are four ways to paint the string, as follows:
- cabaacba
- cabaacba
- cabaacba
- cabaacba
Sample Input 2 Copy
11
mippiisssisssiipsspiim
Sample Output 2 Copy
504
Sample Input 3 Copy
4
abcdefgh
Sample Output 3 Copy
0
Sample Input 4 Copy
18
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Sample Output 4 Copy
9075135300
The answer may not be representable as a 3232-bit integer.
如果直接暴力是O(2^2n)的复杂度,肯定超时,测试样例就可以直接测试出超时i,所以要缩短复杂度,这道题很巧妙,可以把字符串分为两半,这样复杂度就是O(2^n)的了,为什么要分为两部分呢?
先回到题目要求,题目要求就是把字符串分为相等的两个字符串(当然不是从中部分开),一部分从左往右选取n个,一部分从右往左选取n个,这两个字符串相等。
对照第一个样例,可以设这两个字符串为a(红色)和ra(蓝色),把原字符串从中部分开,左边包含蓝色和红色,右边也包含蓝色和红色,a和ra(倒序选取的)是相等的,所以a在左边的部分等于ra在右边的部分,a在右边的部分等于ra在左边的部分,caba acba,a的左边部分是"c",ra的右边部分也是"c",a的右边部分是"aba",ra的左边部分也是"aba",其实很好想象,如果存在a和ra相等,那么一定满足这个规律,左右两半的长度都为n,a和ra的长度也为n,如果a在左边的长度as(<=n),那么左边剩下的(n - as)个字符组成的反向字符串肯定是和a在右边的剩余部分相等的,首先长度肯定是相等,其次内容,因为a和ra都是按照顺序选取并组合的字符串,所以内容肯定也是相等的。因此可以建立一个状态pair<a,ra>,左右状态是相同的就是满足的。
这样只需要记录左边的状态,状态相同的算到一起,红色和蓝色的顺序是无关的,比如caaa acaa和caaa acaa,左边状态是相同的,所以再看右部遇到状态相同的,只需要加上左边相同状态的个数就可以了,遍历直接按照二进制位计算就可以了,区间[0,1<<n)。
c++代码:
#include <map>
#include <iostream>
using namespace std;
typedef pair<string,string> pa;
int main() {
int n;
long long ans = ;
string s;
map<pa,int> mp;
cin>>n>>s;
for(int i = ;i < << n;i ++) {
string a = "";
string b = "";
for(int j = ;j < n;j ++) {
if(i >> j & )a += s[j];///二进制位为1 属于a串
else b += s[j];///二进制位为0 属于ra串
}
mp[pa(a,b)] ++;///记录状态个数
}
for(int i = ;i < << n;i ++) {
string a = "";
string b = "";
for(int j = ;j < n;j ++) {
if(i >> j & )a += s[n * - - j];///二进制位为1 属于a串
else b += s[n * - - j];///二进制位为0 属于ra串
}
ans += mp[pa(a,b)];///加上匹配的状态个数
}
cout<<ans;
}
java代码:
import java.util.*;
class Pair<V,K>{
V first;
K second;
public Pair() {first = null;second = null;}
public Pair(V f,K s){
first = f;
second = s;
}
public boolean equals(Object o) {
if(!(o instanceof Pair))
{
return false;
}
Pair<V,K> pn = (Pair<V,K>)o;
return pn.first.equals(first) && pn.second.equals(second);
}
public int hashCode() {
return first.hashCode() + second.hashCode();
}
}
public class Main { public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long ans = 0;
int n = in.nextInt();
String s = in.next();
String aa = null,bb = null;
Map<Pair<String,String>,Integer> map = new HashMap();
for(int i = 0;i < 1 << n;i ++) {
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
for(int j = 0;j < n;j ++) {
if((i >> j) % 2 == 1)a.append(s.charAt(j));
else b.append(s.charAt(j));
}
aa = a.toString();
bb = b.toString();
if(map.containsKey(new Pair(aa,bb)))map.put(new Pair(aa,bb),map.get(new Pair(aa,bb)) + 1);
else map.put(new Pair(aa,bb),1);
}
for(int i = 0;i < 1 << n;i ++) {
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
for(int j = 0;j < n;j ++) {
if((i >> j) % 2 == 1)a.append(s.charAt(n * 2 - 1 - j));
else b.append(s.charAt(n * 2 - 1 - j));
}
aa = a.toString();
bb = b.toString();
if(map.containsKey(new Pair(aa,bb)))ans += map.get(new Pair(aa,bb));
}
System.out.println(ans);
}
}
AtCoder Grand Contest #026 C - String Coloring的更多相关文章
- AtCoder Grand Contest 026 D - Histogram Coloring
一列中有两个连续的元素,那么下一列只能选择选择正好相反的填色方案(因为连续的地方填色方案已经确定,其他地方也就确定了) 我们现将高度进行离散化到Has数组中,然后定义dp数组 dp[i][j] 表示前 ...
- AtCoder Grand Contest 026 (AGC026) E - Synchronized Subsequence 贪心 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC026E.html 题目传送门 - AGC026E 题意 给定一个长度为 $2n$ 的字符串,包含 $n$ ...
- AtCoder Grand Contest 025 B - RGB Coloring
B - RGB Coloring 求ax + by = k (0<=x<=n && 0<=y<=n)的方案数,最后乘上C(n, x)*C(n,y) 代码: #i ...
- AtCoder Grand Contest #026 B - rng_10s
Time Limit: 2 sec / Memory Limit: 1024 MB Score : 600600 points Problem Statement Ringo Mart, a conv ...
- AtCoder Grand Contest #026 A - Colorful Slimes 2
Time Limit: 2 sec / Memory Limit: 1024 MB Score : 200200 points Problem Statement Takahashi lives in ...
- AtCoder Grand Contest 030 (AGC030) C - Coloring Torus 构造
原文链接https://www.cnblogs.com/zhouzhendong/p/AGC030C.html 题解 才发现当时是被题意杀了. 当时理解的题意是“对于任意的 (i,j) ,颜色 i 和 ...
- Atcoder Grand Contest 026 (AGC026) F - Manju Game 博弈,动态规划
原文链接www.cnblogs.com/zhouzhendong/AGC026F.html 前言 太久没有发博客了,前来水一发. 题解 不妨设先手是 A,后手是 B.定义 \(i\) 为奇数时,\(a ...
- AtCoder Grand Contest 005
AtCoder Grand Contest 005 A - STring 翻译 给定一个只包含\(ST\)的字符串,如果出现了连续的\(ST\),就把他删去,然后所有位置前移.问最后剩下的串长. 题解 ...
- AtCoder Grand Contest 007
AtCoder Grand Contest 007 A - Shik and Stone 翻译 见洛谷 题解 傻逼玩意 #include<cstdio> int n,m,tot;char ...
随机推荐
- IDEA小技巧-随时更新
© 版权声明:本文为博主原创文章,转载请注明出处 1.设置删除一行快捷键 File->Settings->keymap->Delete Line 2.设置代码提示快捷键 File-& ...
- Chrome禁用NPAPI插件(包含 Silverlight、Java 和 Unity)
过去,很多插件都是使用一种称为NPAPI 的旧系统开发的. 现在,仅仅有少量站点在使用NPAPI 插件,由于这些插件有时会给站点带来安全风险. 为了让用户获得更安全.更高速且更稳定的 Chrome 浏 ...
- Ajax请求的跨域(CORS)问题
用浏览器,通过XHR(XMLHttpRequest)请求向另外一个域名请求数据时.会碰到跨域(CORS)问题. CORS:Cross-Origin Resource Sharing 什么是跨域? 简单 ...
- 2个YUV视频拼接技术
http://blog.csdn.net/huahuahailang/article/details/9040847 2个YUV视频拼接技术 http://zhongcong386.blog.163. ...
- 简单手机端头部设置 及css代码
<html> <head> <title>今日报表</title> <meta http-equiv="Content-Type&quo ...
- html中文件类型的accept属性有哪些
*.3gpp audio/3gpp, video/3gpp 3GPP Audio/Video *.ac3 audio/ac3 AC3 Audio *.asf allpication/vnd.ms-as ...
- iOSPOI检索详细总结
iOS百度地图路径规划和POI检索详细总结 路径规划.png 百度地图的使用 百度地图API的导入网上说了许多坑,不过我遇到的比较少,这里就放两个比较常见的吧.坑一: 奥联WIFI_xcodeproj ...
- 【BZOJ1853/2393】[Scoi2010]幸运数字/Cirno的完美算数教室 DFS+容斥
[BZOJ1853][Scoi2010]幸运数字 Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那 ...
- EasyNVR RTSP摄像机HLS直播服务器中使用Onvif协议获取设备快照
我们知道EasyNVR中可以获取快照信息,之前的文章也说明了EasyNVR是如何进行快照抓取的 这里我们使用另一种方法进行快照的抓取 流程 获取设备能力Capabilities 获取设备的能力,并且可 ...
- Django Web开发指南笔记
Django Web开发指南笔记 语句VS表达式 python代码由表达式和语句组成,由解释器负责执行. 主要区别:表达式是一个值,它的结果一定是一个python对象:如:12,1+2,int('12 ...