BNUOJ 3580 Oulipo
Oulipo
64-bit integer IO format: %lld Java class name: Main
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:
Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.
So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.
Input
The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:
- One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
- One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.
Output
For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.
Sample Input
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
Sample Output
1
3
0
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
const int MaxN = ;
char word[MaxN/], txt[MaxN];
int next[MaxN/];
void KMP_next(char b[], int pre[]) {
int n = strlen(b), k;
pre[] = -;
k = -;
for(int i = ; i < n; i++) {
while(k > - && b[k+] != b[i]) k = pre[k];
if(b[k+] == b[i]) k++;
pre[i] = k;
}
} int main() {
int n;
scanf("%d%*",&n);
while(n--) {
gets(word);
gets(txt);
KMP_next(word, next);
int cnt = , len = strlen(word);
for(int i = , j = -; txt[i]; ++i) {
while(j > - && word[j+] != txt[i]) j = next[j];
if(word[j+] == txt[i]) j++;
if(j == len-) {
cnt++;
j = next[j];
}
}
printf("%d\n", cnt);
}
return ;
}
Source
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = ;
int fail[maxn];
void getNext(const char *pStr, int *nextArr) {
int i = , j = -, pLen = strlen(pStr);
nextArr[i] = j;
while (i < pLen) {
if (pStr[++i] != pStr[++j]) {
nextArr[i] = j;
while (j != - && pStr[i] != pStr[j]) j = nextArr[j];
} else nextArr[i] = nextArr[j];
}
}
char word[maxn],text[maxn];
int main(){
int n,ret;
scanf("%d",&n);
while(n--){
scanf("%s %s",word,text);
getNext(word,fail);
for(int i = ret = ,j = ; text[i]; ++i){
while(j != - && word[j] != text[i]) j = fail[j];
if(!word[++j]) ret++;
}
printf("%d\n",ret);
}
return ;
}
整理以后的,可以选择开启所谓的优化
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn = ;
int fail[maxn];
char word[maxn],text[maxn];
void getFail() {
fail[] = -;
fail[] = ;
for(int i = ,j = -; word[i]; ++i) {
while(j != - && word[i] != word[j]) j = fail[j];
fail[i+] = ++j;
if(word[i+] == word[j]) fail[i+] = fail[j];//使用此句加优化,注释掉不加优化,都是正确的
}
}
int main() {
int n,ret;
scanf("%d",&n);
while(n--) {
scanf("%s%s",word,text);
getFail();
for(int i = ret = , j = ; text[i] ; ++i) {
while(j > - && word[j] != text[i]) j = fail[j];
if(!word[++j]) {ret++;j = fail[j];}
}
printf("%d\n",ret);
}
return ;
}
BNUOJ 3580 Oulipo的更多相关文章
- C++之路进阶——poj3461(Oulipo)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35694 Accepted: 14424 Descript ...
- poj3461 Oulipo(KMP模板)
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17795 Accepted: 7160 Descripti ...
- BNUOJ 52325 Increasing or Decreasing 数位dp
传送门:BNUOJ 52325 Increasing or Decreasing题意:求[l,r]非递增和非递减序列的个数思路:数位dp,dp[pos][pre][status] pos:处理到第几位 ...
- Match:Oulipo(POJ 3461)
Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...
- bnuoj 24251 Counting Pair
一道简单的规律题,画出二维表将数字分别相加可以发现很明显的对称性 题目链接:http://www.bnuoj.com/v3/problem_show.php?pid=24251 #include< ...
- KMP算法 hdu4686 Oulipo
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- hdu----1686 Oulipo (ac自动机)
Oulipo Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- android开发学习 ------- 【转】 android中的线程池
线程很常见 , https://blog.csdn.net/seu_calvin/article/details/52415337 参考,保证能看懂.
- IT人怎样防止过劳死?如何成为时间的主人?
投行的朋友还没走几天,搜狐的一位同胞又去了.又是过劳死! 每当读到这类新闻,IT人无不反镜自照,顾影自怜.无法拼爹拼钱的我们,似乎只有拼命了.生活好惨淡啊! 有人说:年轻人,悠着点儿!立刻 ...
- react项目构建中的坑—淘宝镜像安装后要设置
基本的搭建步骤参考博客:https://blog.csdn.net/qq_27727251/article/details/86593415 这里要强调的坑是:安装完淘宝镜像要将其设置为默认Regis ...
- 屏幕旋转时 Activity 的生命周期 —— 测试与结论
关于 Android 手机横竖屏切换时 Activity 的生命周期问题,网上有很多相似的文章,大多数都是说明在竖屏切换横屏时 Activity 会重启一次,而在横屏切换竖屏时 Activity 会重 ...
- svn亲笔操作
1. 创建版本库 [root@iZ28dftuhfaZ db]# svnadmin create /var/svn-repositories/app-api/ . 导入数据到你的版本库[root@iZ ...
- JS 操作对象 事件 样式
1.获取标记对象 css 1 - class 2 - id 3 - 标记选择器 js 1 - class 2 - id 3 - 标记 4 - name + document.getElementByI ...
- ajax传给springMVC数据编码集问题
前台 ajax: $.ajax("${pageContext.request.contextPath}/hello",// 发送请求的URL字符串. { dataType : &q ...
- Prim算法解决最小生成树
一.最小生成树问题 什么是最小生成树问题?给你一个带权连通图,需要你删去一些边,使它成为一颗权值最小的树. 二.Prim算法 1)输入:输入一个带权连通图,顶点集合V,边集合E 2)初始化:Vnew= ...
- CPP-网络/通信:POST
BOOL PostSubmit(CString strUrl,const CString&strPara, CString&strContent){ BOOL bRet=FALSE; ...
- C++ C++ 值传递、指针传递、引用传递详解
这一篇博客写的不错: https://www.cnblogs.com/dingxiaoqiang/p/8012578.html