【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 ...
随机推荐
- word ppt excel文档转换成pdf
1.把word文档转换成pdf (1).添加引用 using Microsoft.Office.Interop.Word; 添加引用 (2).转换方法 /// <summary> /// ...
- 找不到可安装的ISAM
转载:http://www.cnblogs.com/zyc2/archive/2005/06/28/182492.html 读取excel数据 到 datagrid 出现:找不到可安装的ISAM ...
- 20160409 javaweb 数据库连接池
1.自己编写数据库连接池: package com.dzq.pool; import java.io.PrintWriter; import java.lang.reflect.InvocationH ...
- Angularjs中使用$location获取url参数时,遇到的坑~~~
今天在开发时候,需要用到Angularjs1.4.6获取url参数,网上查了一下,有部分文章提到用$location来获取.大致方法如下 var app = angular.module('myApp ...
- works-er
- Visual C++ 打印编程技术-打印基础知识
打印机介绍 1.打印术语 *: 1 英寸= 2.54 厘米(cm)= 25.4 毫米(mm) cpi (Characters Per Inch): 每英寸内所含的字符数,用来表示字符的大小.间距 cp ...
- UVA 11732 strcmp() Anyone?(Trie的性质)
strcmp() Anyone? strcmp() is a library function in C/C++ which compares two strings. It takes two st ...
- 九度OJ 1079 手机键盘
题目地址:http://ac.jobdu.com/problem.php?pid=1079 题目描述: 按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次, ...
- linux 信号处理
查看信号 kill -l 信号实际就是一个进程发送给另一个进程的消息
- C# Linq To DataTable 分组统计 DEMO
DataTable dt = SQLLayer.Get工作量统计(beginDate, endDate); var querySum = from t in dt.AsEnum ...