【CF】86 B. Petr#
误以为是求满足条件的substring总数(解法是KMP分别以Sbeg和Send作为模式串求解满足条件的position,然后O(n^2)或者O(nlgn)求解)。
后来发现是求set(all valid substring), O(n^2)遍历起始和终止位置并利用set肯定超时。因此,利用字符串hash求解。
/* 113B */
#include <iostream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int prime = ;
const int maxn = ;
char s[maxn];
char s1[maxn];
char s2[maxn];
int nxt1[maxn];
int nxt2[maxn];
bool valid1[maxn];
bool valid2[maxn];
__int64 Pow[maxn];
__int64 hash[maxn]; void getNext(char *s, int nxt[]) {
int slen = strlen(s);
int i = , j = -; nxt[] = -;
while (i < slen) {
if (j==- || s[i]==s[j]) {
++i;
++j;
nxt[i] = j;
} else {
j = nxt[j];
}
}
} void kmp(char *s, char *d, int nxt[], bool valid[]) {
int slen = strlen(s);
int dlen = strlen(d);
int i = , j = ; while (i < dlen) {
if (d[i] == s[j]) {
++i;
++j;
} else {
j = nxt[j];
if (j == -) {
j = ;
++i;
}
}
if (j == slen) {
valid[i-slen] = true;
j = nxt[j];
}
}
} void init() {
Pow[] = ;
rep(i, , maxn)
Pow[i] = Pow[i-] * prime; int slen = strlen(s);
hash[] = s[] - 'a' + ;
rep(i, , slen)
hash[i] = hash[i-] * prime + s[i]-'a'+;
} __int64 getHash(int b, int e) {
return hash[e] - (b== ? 0LL : hash[b-]) * Pow[e-b+];
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif scanf("%s %s %s", s, s1, s2); init();
getNext(s1, nxt1);
getNext(s2, nxt2); int i, j, k;
int slen = strlen(s);
int slen1 = strlen(s1);
int slen2 = strlen(s2); kmp(s1, s, nxt1, valid1);
kmp(s2, s, nxt2, valid2); vector<__int64> vc; for (i=; i<slen; ++i) {
if (!valid1[i])
continue;
for (j=i; j<slen; ++j) {
if (valid2[j] && i+slen1<=j+slen2) {
vc.pb(getHash(i, j+slen2-));
}
}
} sort(all(vc));
int sz = SZ(vc);
int ans = ; for (i=sz-; i>=; --i) {
if (i== || vc[i]!=vc[i-])
++ans;
}
printf("%d\n", ans); #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}
【CF】86 B. Petr#的更多相关文章
- 【LeetCode】86. Partition List 解题报告(Python)
[LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【LeetCode】86. 分隔链表
86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...
- 【CF】438E. The Child and Binary Tree
http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...
- 【CF】148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...
- 【CF】328 D. Super M
这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...
- 【CF】323 Div2. D. Once Again...
挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...
- 【CF】7 Beta Round D. Palindrome Degree
manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...
- 【CF】121 Div.1 C. Fools and Roads
题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...
- 【CF】310 Div.1 C. Case of Chocolate
线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...
随机推荐
- Sql遍历更新脚本
DECLARE My_Cursor CURSOR --定义游标 declare@indexId int FOR (SELECT * FROM dbo.GalleryPhoto) --查出需要的集合放到 ...
- python杂乱有关类与对象
在python的类中,一般里面的方法和属性默认都是publlic,如果要设为private,即自己访问,主要是根据命名方式来区分的,通过__ 两个下划线加在名字前面,即为私有方法和私有属性 构造方法 ...
- 双程动态规划 nyoj61
题目大意: 在矩阵m*n中,从(1,1)点到(m,n)点,再从(m,n)点到(1,1)点,所走路线经过的同学最大好心值, 要求每个点只能走一遍. 分析: ①我们可以把它只看成两个人同时从(1,1)点, ...
- ACM——圆柱体的表面积
lems 1092 圆柱体的表面积 时间限制(普通/Java):1000MS/3000MS 运行内存限制:65536KByte总提交:2697 测试通过:414 ...
- 使用PDO连接多种数据库
在PHP 5之前,想要连接MySQL数据库就需要使用mysql或mysqli等一系列函数来操作数据库.例如,我们使用mysql系列数据库函数进行查询操作,对应的示例代码如下: <?php //创 ...
- ns2出现Client: Handoff Attempt的情况解决
找到mac/mac-802_11.cc,这是系统本身一个bug,对于adhoc网络无需进行切换尝试. > if (*rcount == 3 && handoff == 0) {& ...
- phaser源码解析(二) Phaser.Utils类下pad方法
/** *#填充字符串方法 * Javascript string pad http://www.webtoolkit.info/. * pad = the string to pad it out ...
- discuz注册 内部错误
ucenter整合后,在论坛注册时出现 内部错误,无法显示,这是因为ucenter中,某个appid没写入! 可以把ucenter安装包下的utilities/checkappid.php,复制到uc ...
- jsp 嵌套iframe 从iframe中表单提交并传值到外层
今天因需求迭代 更改元来代码 遇到了这么个问题 就是想在 iframe中提交后进行整个页面的跳转 并把iframe中的值传到外层jsp 大概就是这个样子 外层 a.jsp <div id=&qu ...
- jquery 多级无限分类
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...