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 ...
随机推荐
- English trip M1 - PC12 I'd Like a Room Please Teacher:Taalan
In this lesson you will learn to say what you need. 在本课中,您将学习如何说出您的需求. Words list elevator 电梯 ...
- 170301、使用Spring AOP实现MySQL数据库读写分离案例分析
使用Spring AOP实现MySQL数据库读写分离案例分析 原创 2016-12-29 徐刘根 Java后端技术 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案 ...
- Oracle11g温习-第六章:控制文件
2013年4月27日 星期六 10:33 .控制文件的功能和特点 1) [定义数据库当前物理状态] 2) [维护数据的一致性] 如果控制文件中的检查点与数据文件中的一致,则说明数据一致,可以启动到 ...
- java plsql 调用oracle数组类型
首先当然是在oracle中建立type CREATE OR REPLACE TYPE cux_proxy_bid_award_rec IS OBJECT ( trading_partner_id NU ...
- HTML <a> 标签的伪类
伪类的语法: selector : pseudo-class {property: value} CSS 类也可与伪类搭配使用. selector.class : pseudo-class {prop ...
- Pandas DataFrame 数据选取和过滤
This would allow chaining operations like: pd.read_csv('imdb.txt') .sort(columns='year') .filter(lam ...
- Vysor安装
Vysor安装 Vysor安装
- 关于std::map的第三个参数
1.map的其中一个构造函数有第三个参数,可以直接定义map的key值得排序规则, 默认为std::less,即按“<”运算符进行排序 map<string, int> mapWor ...
- 51nod1269Devu and Flowers
题解: 如果没有限制每一种花有多少,那么就是简单的排列组合问题. 那么我们强制让一些花一定都要选. 暴力搜索,然后组合数(逆元) 采用容斥原理来计算最后的答案 代码: #include<bits ...
- BLE低功耗蓝牙关键技术解析与应用
BLE基础知识 1.传统蓝牙的传输距离几十米到几百米不等,BLE 则规定为 100 米(实际上没有那么远,50米以内比较稳定,和设备发射功率有关) 2.为了实现极低的功耗,BLE 协议设计为:在不必要 ...