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 ...
随机推荐
- Anton and School - 2 CodeForces - 785D (组合计数,括号匹配)
大意: 给定括号字符串, 求多少个子序列是RSGS. RSGS定义如下: It is not empty (that is n ≠ 0). The length of the sequence is ...
- zabbix3.0.4 配置邮件报警
试验环境: LAMP环境 (LNMP环境已经成功了,为了避免干扰,我另一台LAMP主机) ### 我在做实验之前,作了时间同步,不知道这个有木有影响,一起说一下吧! yum -y install nt ...
- C++面试问题详解
1.定义一个全局变量放在.cpp文件还是.h文件,原因是什么 在cpp文件中定义变量,h文件用来声明变量的作用域,使用extern声明的变量可以在本编译单元或其他编译单元中使用. 举例如下: a.h文 ...
- HDOJ1008
#include "iostream" using namespace std; int main() { ) { int n; cin >> n; ) break; ...
- iOS UI-常用控件
#import "ViewController.h" @interface ViewController ()<UITextFieldDelegate> // 标题标签 ...
- Python中变量、赋值、浅拷贝、深拷贝
https://www.cnblogs.com/LetMe/p/6724555.html 在理解浅拷贝和深拷贝之前,首先要理解学习一下变量在Python中是怎样存储的: 变量的类型是分值引用与地址引用 ...
- oracle图形界面配置tns
oracle图形界面配置tns 启动orcl服务
- 远程桌面连接 [Content] 出现身份验证错误。 要求的函数不受支持
[Window Title] 远程桌面连接 [Content] 出现身份验证错误. 要求的函数不受支持 以上是我远程得时候报的错. 下面直接上 最NB得解决方案.不管用直接在下面评论 通过管理控 ...
- bzoj1091
题解: 暴力枚举顺序 然后计算几何 代码: #include<bits/stdc++.h> ],lp=; double v1,v2,ans=1e10; struct pos { doubl ...
- prayer OJ M
这一题是一把辛酸泪啊...一个半小时ac的... 首先,考虑到如果要一条路径最小,那么肯定是没有值大于等于3的 显然如果有一个大于等于3的,那么这个数把路径分成两份,一份有k个,一个n-k-1个 那么 ...