test20180922 交错的字符串
题意

分析
这个数据范围容易使人想到折半搜索。
我们将字符串分为前后两部分。如果前半部分中搜得的前缀串为{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 交错的字符串的更多相关文章
- Codeforces #505(div1+div2) C Plasticine zebra
题意:给你一段字符串,可以选择任意多的位置,每个位置会反转两边的字符串,问交错的字符串最长是多长? 思路:找规律,仔细分析样例1.假设位置为 1 2 3 4 5 6 7 8 9,反转之后会发现答案是7 ...
- NOIp2018集训test-9-22(am/pm) (联考三day1/day2)
szzq学长出的题,先orz一下. day1 倾斜的线 做过差不多的题,写在我自己的博客里,我却忘得一干二净,反而李巨记得清清楚楚我写了的. 题目就是要最小化这个东西 $|\frac{y_i-y_j} ...
- [Swift]LeetCode97. 交错字符串 | Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...
- LeetCode(97):交错字符串
Hard! 题目描述: 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = &qu ...
- LeetCode-97.交错字符串
给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = "dbbca&quo ...
- 097 Interleaving String 交错字符串
给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的.例如,给定:s1 = "aabcc",s2 = "dbbca",当 s ...
- 【leetcode-97 动态规划】 交错字符串
(1过,调试很久) 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = " ...
- Java实现 LeetCode 97 交错字符串
97. 交错字符串 给定三个字符串 s1, s2, s3, 验证 s3 是否是由 s1 和 s2 交错组成的. 示例 1: 输入: s1 = "aabcc", s2 = " ...
- [leetcode]97. Interleaving String能否构成交错字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Input: s1 = "aabc ...
随机推荐
- Html概述
Html概述 主要思想,主要思想很重要 一.Html是什么 一种标记语言(用标签进行标记) 对要显示的文字进行标记 二.Html核心 标签(只认标签) 封装,所以必须有头尾,你才知道标签的范围,你才知 ...
- Xshell Xftp 免费版 (xshell6 评估期已过 解决办法)
xshell6 评估期已过,因为下载的版本是evaluation版本,是有期限的. 大家可以修改为Home and school use 的版本,这样就不会出现这个提示了. 具体的操作步骤如下: 1. ...
- android--------动画之进度条
Android开发中在处理耗时工作的时候,例如:列表加载,大多数会有一个精度条加载的框,里面有一个像gif的图片在旋转一样. 效果图: <!-- 根标签为animation-list ...
- Python基础--Python简介和入门
☞写在前面 在说Python之前,我想先说一下自己为什么要学Python,我本人之前也了解过Python,但没有深入学习.之前接触的语言都是Java,也写过一些Java自动化用例,对Java语言只能说 ...
- HDU-4856 Tunnels (BFS+状压DP)
Problem Description Bob is travelling in Xi’an. He finds many secret tunnels beneath the city. In hi ...
- iOS UI-集合视图(UICollectionView)
BowenCollectionViewCell.xib #import <UIKit/UIKit.h> @interface BowenCollectionViewCell : UICol ...
- learning ext2 filesystem notes
reference: http://e2fsprogs.sourceforge.net/ext2intro.html reference: http://www.nongnu.org/ext2-do ...
- 51nod1269Devu and Flowers
题解: 如果没有限制每一种花有多少,那么就是简单的排列组合问题. 那么我们强制让一些花一定都要选. 暴力搜索,然后组合数(逆元) 采用容斥原理来计算最后的答案 代码: #include<bits ...
- 递归算法,如何把list中父子类对象递归成树
以前写代码for循环写的多,递归除了在大学学习以外,真没怎么用过! 最近项目中使用到了关于族谱排列的问题,就是怎么把数据库里的多个子父类people对象,在界面中用树的结构展示出来 假设数据库中peo ...
- 驱动程序多线程 PsCreateSystemThread
内核函数PsCreateSystemThread负责创建新线程.该函数可以创建两种线程,一种是用户线程,它属于当前进程中的线程.另一种是系统线程,系统线程不属于当前用户进程,而是属于系统进程,一般PI ...