题意

分析

这个数据范围容易使人想到折半搜索。

我们将字符串分为前后两部分。如果前半部分中搜得的前缀串为{S1, S2},那么后半部分中搜得的后缀串必须为{rev(S2), rev(S1)},且为有序对。对于两侧分别枚举每个字符的归属情况,hash后用map计数即可。

代码

#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<ctime>
#include<iostream>
#include<string>
#include<vector>
#include<list>
#include<deque>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<complex>
#pragma GCC optimize ("O0")
using namespace std;
template<class T> inline T read(T&x)
{
T data=0;
int w=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')
w=-1;
ch=getchar();
}
while(isdigit(ch))
data=10*data+ch-'0',ch=getchar();
return x=data*w;
}
typedef long long ll;
const int INF=0x7fffffff; int n;
string S;
string S1,S2;
map<string,ll>M; int main()
{
freopen("string.in","r",stdin);
freopen("string.out","w",stdout);
cin>>n>>S;
int ms=1<<n;
for(int s=0;s<ms;++s)
{
S1.clear(); // s1
S2.clear(); // s2
for(int i=0;i<n;++i)
{
if(s&(1<<i))
S1.push_back(S[i]);
else
S2.push_back(S[i]);
}
reverse(S2.begin(),S2.end());
++M[S1+'$'+S2]; // s1+revs2
}
ll ans=0;
for(int s=0;s<ms;++s)
{
S1.clear(); // revs1
S2.clear(); // revs2
for(int i=0;i<n;++i)
{
if(s&(1<<i))
S1.push_back(S[i+n]);
else
S2.push_back(S[i+n]);
}
reverse(S1.begin(),S1.end());
ans+=M[S1+'$'+S2]; // s1+revs2
}
cout<<ans/2<<endl;
// fclose(stdin);
// fclose(stdout);
return 0;
}

test20180922 交错的字符串的更多相关文章

  1. Codeforces #505(div1+div2) C Plasticine zebra

    题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...

  2. NOIp2018集训test-9-22(am/pm) (联考三day1/day2)

    szzq学长出的题,先orz一下. day1 倾斜的线 做过差不多的题,写在我自己的博客里,我却忘得一干二净,反而李巨记得清清楚楚我写了的. 题目就是要最小化这个东西 $|\frac{y_i-y_j} ...

  3. [Swift]LeetCode97. 交错字符串 | Interleaving String

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  4. LeetCode(97):交错字符串

    Hard! 题目描述: 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = &qu ...

  5. LeetCode-97.交错字符串

    给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = "dbbca&quo ...

  6. 097 Interleaving String 交错字符串

    给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...

  7. 【leetcode-97 动态规划】 交错字符串

    (1过,调试很久) 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = " ...

  8. Java实现 LeetCode 97 交错字符串

    97. 交错字符串 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = " ...

  9. [leetcode]97. Interleaving String能否构成交错字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...

随机推荐

  1. English trip -- VC(情景课)8 D Reading

    Listen and read. Shop Smart [smɑːt]  Employee of the Month: Sara['særə] (萨拉) Lopez(洛佩斯) Congratulati ...

  2. CentOS 配置Tomcat服务脚本

    #!/bin/bash # description: Tomcat7 Start Stop Restart # processname: tomcat7 # chkconfig: JAVA_HOME= ...

  3. Android之EventBus1.0 和EventBus3.0的使用详解

    当Android项目越来越庞大的时候,应用的各个部件之间的通信变得越来越复杂,那么我们通常采用的就是Android中的解耦组件EventBus.EventBus是一款针对Android优化的发布/订阅 ...

  4. python-day27--hashlib模块-摘要算法

    1.用途: # 文件校验 # 文件是否被改变# 登录密码 #不能解密,但可以“撞库” #加盐 hashlib.md5('nezha'.encode('utf-8')) 2. import hashli ...

  5. nyoj-310-河南省第四届省赛题目-二分+dinic

    SECRET 时间限制:3000 ms  |  内存限制:65535 KB 难度:6   描述 Dr.Kong is constructing a new machine and wishes to ...

  6. WinForm下的TabControl控件

    一.TabControl控件介绍 TabControl实现的具体效果: 在实际工作中,我是这么用TabControl控件,实现切换页面效果.比如要实现某个界面进行操作,然后还要查看一下日志,就可以使用 ...

  7. 【转】C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码

    本文介绍在 C# 中使用 ThoughtWorks.QRCode.dll 生成指定尺寸和边框宽度的二维码.网上文章大多只是简单介绍内置参数的设置,根据我的使用目的,增加了自定义目标二维码图片尺寸和白边 ...

  8. python 使用yield进行数据的流式处理

    demo:从文件中取包含字符“a”的5行数据做一次批处理!!! # coding: utf-8 import time def cat(f): for line in f: yield line de ...

  9. javascript的逼格

    1.解释性脚本语言,无需编译,逐行解释运行 2.跨平台性,不依赖操作系统,只需要浏览器支持 javascript引擎:单线程

  10. 简话Angular 06 Angular自定义指令

    一句话: 直接return link函数可以解决大多数问题,无须死扣用法 1. 上源码 (dom操作,事件,css,mode操作全包括了) <h3>Custom directive, wi ...