Oulipo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7141    Accepted Submission(s): 2835

Problem 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
3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
 
Sample Output
1
3
0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 1000100
  4. char p[10010],str[MAX];
  5. int f[10010];
  6. void getfail()
  7. {
  8. int i,j,len;
  9. len=strlen(p);
  10. f[0]=f[1]=0;
  11. for(i=1;i<len;i++)
  12. {
  13. j=f[i];
  14. while(j &&p [i]!=p[j])
  15. j=f[j];
  16. f[i+1]=p[i]==p[j]?j+1:0;
  17. }
  18. }
  19. int main()
  20. {
  21. int n,m,j,i,t;
  22. int l1,l2;
  23. scanf("%d",&t);
  24. while(t--)
  25. {
  26. scanf("%s%s",p,str);
  27. getfail();
  28. l1=strlen(p);
  29. l2=strlen(str);
  30. int s=0,j=0;
  31. for(i=0;i<l2;i++)
  32. {
  33. while(j&&str[i]!=p[j])
  34. j=f[j];
  35. if(str[i]==p[j])
  36. j++;
  37. if(j>=l1)
  38. {
  39. s++;
  40. j=f[j];//注意此处
  41. }
  42. }
  43. printf("%d\n",s);
  44. }
  45. return 0;
  46. }

失配函数优化写法:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #define MAX 10010
  4. int ans,len1,len2;
  5. char s[MAX],str[MAX*100];
  6. int f[MAX];
  7. void huang()
  8. {
  9. int i=0,j = -1;
  10. f[i]=j;
  11. while(i < len1)
  12. {
  13. if(j == -1||s[i]==s[j])
  14. {
  15. i++;
  16. j++;
  17. f[i]=j;
  18. }
  19. else
  20. j=f[j];
  21. }
  22. }
  23. void kmp()
  24. {
  25. huang();
  26. int i=0,j=0;
  27. for(i=0;i<len2;i++)
  28. {
  29. while(j && str[i] != s[j])
  30. j = f[j];
  31. if(str[i]==s[j])
  32. j++;
  33. if(j >= len1)
  34. {
  35. ans++;
  36. j = f[j];
  37. }
  38. }
  39. }
  40. int main()
  41. {
  42. int i,t;
  43. scanf("%d",&t);
  44. while(t--)
  45. {
  46. scanf("%s%s",s,str);
  47. len1=strlen(s);
  48. len2=strlen(str);
  49. huang();
  50. ans=0;
  51. kmp();
  52. printf("%d\n",ans);
  53. }
  54. return 0;
  55. }

  

 

 借着此题,写下求失配函数的模板:

  1. /*
  2. * f[]为失配函数数组
  3. *
  4. */
  5. getfail()
  6. {
  7. int i,j;
  8. int len = strlen(p);
  9. f[0]=f[1]=1;
  10. for(i = 1; i < len; i++)
  11. {
  12. j = f[i];
  13. while(j && p[i] != p[j])
  14. j = f[j];
  15. f[i+1]= p[i] ==p[j]?j+1:0;
  16. }
  17. }

  

hdoj 1686 Oulipo【求一个字符串在另一个字符串中出现次数】的更多相关文章

  1. HDU 1686 - Oulipo - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...

  2. javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数

    javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...

  3. Java实现统计某字符串在另一个字符串中出现的次数

    面试时会经常考这样的题目,估计也不让使用正则表达式.还好这个算法还算简单,不过在草稿纸上写难免会出现运行异常,好吧,面试官赢了,乃们屌丝就实实在在的把代码码出来吧. 谢谢“心扉”对我代码bug的纠正, ...

  4. HDU - 1686 Oulipo KMP匹配运用

    id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...

  5. 我的Java开发学习之旅------>求字符串中出现次数最多的字符串以及出现的次数

    金山公司面试题:一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数 ...

  6. Java求字符串中出现次数最多的字符

    Java求字符串中出现次数最多的字符  [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article/details/51933611      Java ...

  7. 字符串A转换到字符串B,只能一次一次转换,每次转换只能把字符串A中的一个字符全部转换成另一个字符,是否能够转换成功

    public class DemoTest { public static void main(String[] args) { System.)); } /** * 有一个字符串A 有一个字符串B ...

  8. C#算法之判断一个字符串是否是对称字符串

    记得曾经一次面试时,面试官给我电脑,让我现场写个算法,判断一个字符串是不是对称字符串.我当时用了几分钟写了一个很简单的代码. 这里说的对称字符串是指字符串的左边和右边字符顺序相反,如"abb ...

  9. Java-map-第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队。如果该 年没有举办世界杯,则输出:没有举办世界杯。 附:世界杯冠军以及对应的夺冠年份,请参考本章附录。 附录

    第一题 (Map)利用Map,完成下面的功能: 从命令行读入一个字符串,表示一个年份,输出该年的世界杯冠军是哪支球队.如果该 年没有举办世界杯,则输出:没有举办世界杯. 附:世界杯冠军以及对应的夺冠年 ...

随机推荐

  1. iOS程序员的自我修养之道

    新技术的了解渠道 WWDC开发者大会视频 官方文档 General -> Guides -> iOS x.x API Diffs 程序员的学习 iOS技术的学习 官当文档 Sample C ...

  2. Windows phone常用控件之Button

    Button类:表示一个响应 ButtonBase.Click 事件的 Windows 按钮控件. 继承层次结构: 命名空间:    System.Windows.Controls ClickMode ...

  3. 【PHP】 foreach循环中变量引用的一道面试题

    $a = array('a','b','c'); foreach($a as &$v){} foreach($a as $v){ } var_dump($a); 现在.不要打开浏览器,猜测一下 ...

  4. async: false的应用.

    目的: 手机webview中, js ajax请求, success后, 进行window.open 操作 问题: 在Android, IOS均不能执行window.open 解决办法: 设置ajax ...

  5. Eclipse debug调试

    Eclipse debug调试: F5:跳入方法F6:向下逐行调试F7:跳出方法F8:直接跳转到下一个断点

  6. php利用时间生成随机函数

    date("YmdHis",time()); rand();    生成随机数   当括号内无参数时 系统会以当前时间为种子进行随机数的生成 rand(1,10);  括号里面是生 ...

  7. 调整ListBox控件的行间距及设置文本格式

    首先要将该控件的DrawMode属性为OwnerDrawVariable 添加DrawItem重绘事件:private void listBox1_DrawItem(object sender, Dr ...

  8. 机器学习系列(17)_Yelper推荐系统

     1. 我们为什么需要推荐系统?“推荐”可是个当红话题.Netflix愿意用百万美金召求最佳的电影推荐算法,Facebook也为了登陆时的推荐服务开发了上百个项目,遑论现在市场上各式各样的应用都需要个 ...

  9. CodeChef CBAL

    题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...

  10. bzoj 2115: [Wc2011] Xor xor高斯消元

    2115: [Wc2011] Xor Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 797  Solved: 375[Submit][Status] ...