博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oulipo
阅读量:5849 次
发布时间:2019-06-19

本文共 3782 字,大约阅读时间需要 12 分钟。

                                 Oulipo

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30723   Accepted: 12349

Description

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

3BAPCBAPCAZAAZAZAZAVERDIAVERDXIVYERDIAN

Sample Output

1

3

0

 

题意:给若干组数据,每组两个字符串,问第一个字符串在第二个字符串中出现的次数

思路:KMP算法来判断长串中的子串是否和短串相等,

注意:如果发现一组匹配,让 ANS++,j=next[j]+1,i++,因为next[i]=x的意思是以1为起始的前缀和以i为结束的后缀的长度为x,这样可以看出此时P[next[i]]==P[1],这样就避免了让 j=1,i=i-lenP+2,避免了时间的浪费。由KMP的next构造可知,P[10]={\000AAAA}的next={0,0,1,2,3,},P的前缀和后缀是可以重叠的,而且会让每个的next[i]的值尽量大,所以可以保证不会漏掉某一种情况

 

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 char T[1000010],P[10010]; 9 int N,next[10010],lenP,lenT,ans;10 int main(){11 scanf("%d",&N);12 while(N--){13 ans=0;14 scanf("%s%s",P+1,T+1);15 lenP=strlen(P+1); lenT=strlen(T+1);16 //get next[]17 for(int j=0,i=2;i<=lenP;i++){18 while(P[j+1]!=P[i]&&j) j=next[j];19 if(P[j+1]==P[i]) j++;20 next[i]=j;21 }22 23 for(int i=1,j=1;i<=lenT;){24 if(P[j]==T[i]){25 if(j==lenP){26 ans++;27 j=next[j]+1;28 i++;29 }30 else i++,j++;31 }32 else{33 if(j==1) i++;34 else j=next[j-1]+1;35 }36 }37 printf("%d\n",ans);38 }39 return 0;40 }
还可以用hash
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std ; 9 typedef unsigned long long uLL ;10 const int N=1000005 ;11 const uLL Base=1234567 ;12 uLL base[N]={ 1},hs,hA[N] ;13 inline uLL ask(int l,int r)14 { return hA[r]-hA[l-1]*base[r-l+1] ;}15  16 char A[N],s[N] ;17 int T,ans,n,m ;18 int main()19 {20     for(int i=1;i
 

 

 

转载于:https://www.cnblogs.com/CXCXCXC/p/4936469.html

你可能感兴趣的文章
linux 基本命令整理
查看>>
ACCELEROMETER
查看>>
基于JMF RTP的音视频传输
查看>>
Html Agility Pack (HAP) 应用入门
查看>>
[LeetCode] Subsets 子集合
查看>>
The 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the local machine-Excel2003
查看>>
PermGen space错误解决方法
查看>>
转贴一段偶觉得说得非常好的话
查看>>
petri网
查看>>
关于SQLSERVER数据库连接池
查看>>
php 使用curl发起https请求
查看>>
<context-param>与<init-param>的区别与作用及获取方法
查看>>
《Java 2 图形设计卷Ⅱ- SWING》第12章 轻量容器
查看>>
sklearn学习笔记之简单线性回归
查看>>
“爸爸用键盘养活了全家”
查看>>
第 22 章 排版
查看>>
Auty自动化测试框架第一篇——生成执行列表
查看>>
BZOJ 1002: [FJOI2007]轮状病毒【生成树的计数与基尔霍夫矩阵简单讲解+高精度】
查看>>
html元素类型 块级元素、内联元素(又叫行内元素)和内联块级元素。
查看>>
第 1 章 IDC
查看>>