误以为是求满足条件的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#的更多相关文章

  1. 【LeetCode】86. Partition List 解题报告(Python)

    [LeetCode]86. Partition List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 【LeetCode】86. 分隔链表

    86. 分隔链表 知识点:链表: 题目描述 给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前. 你应当 保留 两个 ...

  3. 【CF】438E. The Child and Binary Tree

    http://codeforces.com/contest/438/problem/E 题意:询问每个点权值在 $c_1, c_2, ..., c_m$ 中,总权值和为 $s$ 的二叉树个数.请给出每 ...

  4. 【CF】148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题意:w个白b个黑,公主和龙轮流取,公主先取,等概率取到一个.当龙取完后,会等概率跳出一只.(0<= ...

  5. 【CF】328 D. Super M

    这种图论题已经变得简单了... /* D */ #include <iostream> #include <string> #include <map> #incl ...

  6. 【CF】323 Div2. D. Once Again...

    挺有意思的一道题目.考虑长度为n的数组,重复n次,可以得到n*n的最长上升子序列.同理,也可以得到n*n的最长下降子序列.因此,把t分成prefix(上升子序列) + cycle(one intege ...

  7. 【CF】7 Beta Round D. Palindrome Degree

    manacher+dp.其实理解manacher就可以解了,大水题,dp就是dp[i]=dp[i>>1]+1如何满足k-palindrome条件. /* 7D */ #include &l ...

  8. 【CF】121 Div.1 C. Fools and Roads

    题意是给定一棵树.同时,给定如下k个查询: 给出任意两点u,v,对u到v的路径所经过的边进行加计数. k个查询后,分别输出各边的计数之和. 思路利用LCA,对cnt[u]++, cnt[v]++,并对 ...

  9. 【CF】310 Div.1 C. Case of Chocolate

    线段树的简单题目,做一个离散化,O(lgn)可以找到id.RE了一晚上,额,后来找到了原因. /* 555C */ #include <iostream> #include <str ...

随机推荐

  1. lsjORM ----让开发变得更加快捷(二)

    lsjORM结构 跟传统三层没有多大区别,这里添加DTO(参数列表)跟PetaPoce(数据库操作),普通的三层我们都喜欢用DBHelper或者SqlHelper来封装sql的辅助方法,PetaPoc ...

  2. SQL 有父标识的 递归查询

    递归查询,临时表的高级应用 WITH temp AS ( --父项 SELECT * FROM Ar_Area WHERE Ar_Parent = UNION ALL --递归结果集中的下级 SELE ...

  3. ASP.NET 设计模式(转)

    Professional ASP.NET Design Patterns 为什么学习设计模式? 运用到ASP.NET应用程序中的设计模式.原则和最佳实践.设计模式和原则支持松散耦合.高内聚的代码,而这 ...

  4. 怎样区分JQuery对象和Dom对象 常用的写法

    第一步,http://www.k99k.com/jQuery_getting_started.html 第二步,新手先仔细得全部看一遍jQuery的选择器,很重要!!! (http://shawphy ...

  5. Javascript 迭代法实现数组多条件排序

    多条件排序可能有很多种思路,效率也各不相同,我的方法可能只适合自己用,毕竟目的是为了实现功能,所以采用了最笨的方法,不过效果还是很理想的,经过多次测试,6列1000行数据,平均排序时间大约是:28ms ...

  6. 在Windows下用gSoap实现简单加法实例

    实现一个简单的a+b程序,在服务器端写一个程序,里面包含了a+b的函数,然后通过客户端代码向其发送两个数字,在服务器运算得到结果返回给客户端显示出来. 1.在gSoap的官网上下载文件夹,本人的版本是 ...

  7. VS2010修改默认配置路径

    视图->属性管理器 弹出如下截图:

  8. (转)IOS 学习笔记 2015-03-23 如何获取IOS程序的系统信息

    首页 > 程序开发 > 移动开发 > IOS > 正文 iOS 获取手机的型号,系统版本,软件名称,软件版本 -- 个评论 作者:vipa1888 收藏 我要投稿 网上搜索出来 ...

  9. ObjectQuery查询及方法

    ObjectQuery 类支持对 实体数据模型 (EDM) 执行 LINQ to Entities 和 Entity SQL 查询.ObjectQuery 还实现了一组查询生成器方法,这些方法可用于按 ...

  10. IOS视图旋转可放大缩小

    - (IBAction)hideBut:(id)sender { if (self.flg) { [UIView animateWithDuration:0.3 animations:^{ self. ...