HDU 3746 Cyclic Nacklace ( KMP求最小循环节 )

len - nextval[len]即为最小循环节长度。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int MAXN = ;
  9. const int INF = << ;
  10.  
  11. char str[MAXN];
  12. int nextval[MAXN];
  13. int len;
  14.  
  15. void getNext( char s[], int next[] )
  16. {
  17. int length=len;
  18. int i=,j=-;
  19. next[]=-;
  20. while(i<length)
  21. {
  22. if(j==-||s[i]==s[j])
  23. {
  24. ++i;
  25. ++j;
  26. next[i]=j;
  27. }
  28. else
  29. j=next[j];
  30. }
  31. }
  32.  
  33. int main()
  34. {
  35. int T;
  36. scanf( "%d", &T );
  37. while ( T-- )
  38. {
  39. scanf( "%s", str );
  40. len = strlen(str);
  41. getNext( str, nextval );
  42. int sub = len - nextval[len];
  43. if ( sub != len && len % sub == ) puts("");
  44. else printf( "%d\n", sub - ( nextval[len]%sub ) );
  45. }
  46. return ;
  47. }

HDU 1358 Period

题意:若某串的前i个字符是循环串,输出i以及循环节出现的次数。

做法跟上一题差不多

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int MAXN = ;
  9.  
  10. char str[MAXN];
  11. int nextval[MAXN];
  12. int len;
  13.  
  14. void getNext( char s[], int next[] )
  15. {
  16. int length=len;
  17. int i=,j=-;
  18. next[]=-;
  19. while(i<length)
  20. {
  21. if(j==-||s[i]==s[j])
  22. {
  23. ++i;
  24. ++j;
  25. next[i]=j;
  26. }
  27. else
  28. j=next[j];
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. int cas = ;
  35. while ( scanf( "%d", &len ), len )
  36. {
  37. scanf( "%s", str );
  38. getNext( str, nextval );
  39. printf( "Test case #%d\n", ++cas );
  40. for ( int i = ; i <= len; ++i )
  41. {
  42. int sub = i - nextval[i];
  43. if ( i / sub > && i % sub == )
  44. {
  45. printf("%d %d\n", i, i / sub );
  46. }
  47. }
  48. puts("");
  49. }
  50. return ;
  51. }

POJ 2406 Power Strings(求循环串的最大循环周期)

注意这组数据:aabaabaa

显然答案是:1

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4.  
  5. const int MAXN = ;
  6.  
  7. char str[MAXN];
  8. int nextval[MAXN];
  9. int len;
  10.  
  11. void getNext(char s[],int next[])
  12. {
  13. int length=len;
  14. int i=,j=-;
  15. next[]=-;
  16. while(i<length)
  17. {
  18. if(j==-||s[i]==s[j])
  19. {
  20. ++i;
  21. ++j;
  22. next[i]=j;
  23. }
  24. else
  25. j=next[j];
  26. }
  27. }
  28.  
  29. int main()
  30. {
  31. while ( scanf( "%s", str ) == && str[] != '.' )
  32. {
  33. len = strlen(str);
  34. getNext( str, nextval );
  35. int sub = len - nextval[len];
  36. if ( len % sub == ) printf("%d\n", len / sub );
  37. else printf("1\n");
  38. }
  39. return ;
  40. }

KMP与循环节相关题目的更多相关文章

  1. HDU 3746 Cyclic Nacklace (KMP求循环节问题)

    <题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题]  #include &l ...

  2. KMP解决字符串最小循环节相关问题

    经典问题 : 给出一个由某个循环节构成的字符串,要你找出最小的循环节,例如 abababab 最小循环节当是 ab ,而类似 abab 也可以成为它的循环节,但并非最短. 分析 : 对于上述问题有两个 ...

  3. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  4. HDU 1358 Period(KMP+最小循环节)题解

    思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...

  5. HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解

    思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...

  6. 用KMP征服循环节问题

    以前我还是写过KMP的文章的 现在我们可以求一下循环节啊 Slot Machines Gym - 101667I #include<bits/stdc++.h> using namespa ...

  7. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)

    做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...

  8. (KMP 根据循环节来计算)Period -- hdu -- 1358

    http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  9. ( KMP 求循环节的个数)Power Strings -- poj -- 2406

    链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bi ...

随机推荐

  1. 【转】使用webmagic搭建一个简单的爬虫

    [转]使用webmagic搭建一个简单的爬虫 刚刚接触爬虫,听说webmagic很不错,于是就了解了一下. webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代 ...

  2. cudaMallocPitch()

  3. 第二天了,由于博主太分心了,看看就跑去研究了一下ssh和ufw以及nmap,现在急需记录一下啦,哈哈!

    昨天看到了视频的ssh远程连接,因为我点电脑上装的是一个ubuntu的虚拟机,我根据视频看了一下,自己又试用了一下,我发现自己的ubuntu是能够远程到自己的Mac电脑上,一开始主要是因为自己不能连接 ...

  4. Oracle SCN与时间的相互转换

    1.SCN转换成时间 select scn_to_timestamp(current_scn) from v$database; 2.时间转换成SCN select timestamp_to_scn( ...

  5. 【iOS】史上最全的iOS持续集成教程 (下)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  6. ES6的数组方法之Array.from

    首先说说什么是数组:数组在类型划分上归为Object,属于比较特殊的对象,数组的索引值类似于对象的key值. 数组的几个注意点: 1.数组的长度是可读属性,不可更改,数组的长度根据索引最大值. 2.数 ...

  7. django+xadmin在线教育平台(九)

    django admin介绍 上一章我们进行了需求分析和数据库设计.本章我们来快速搭建一个可用的后台管理系统. 后台管理系统特点: 权限管理 少前端样式.(样式一般不是很看重), 快速开发 djang ...

  8. spring框架中@PostConstruct的实现原理

    在spring项目经常遇到@PostConstruct注解,首先介绍一下它的用途: 被注解的方法,在对象加载完依赖注入后执行. 此注解是在Java EE5规范中加入的,在Servlet生命周期中有一定 ...

  9. c#字符显示转换{0:d} string.Format()

    这一篇实际和前几个月写的没什么本质上的区别.但是这篇更明确一点,学起来easy c#字符显示转换{0:d} C#:String.Format数字格式化输出 : int a = 12345678; // ...

  10. python--Matplotlib(二)

    Matplotlib+pandas作图 一.对csv文件进行提取ruixi.csv 对上述表格进行提取并做图 #-*- coding:utf-8 -*- import matplotlib as mp ...