KMP与循环节相关题目
HDU 3746 Cyclic Nacklace ( KMP求最小循环节 )
len - nextval[len]即为最小循环节长度。
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- const int MAXN = ;
- const int INF = << ;
- char str[MAXN];
- int nextval[MAXN];
- int len;
- void getNext( char s[], int next[] )
- {
- int length=len;
- int i=,j=-;
- next[]=-;
- while(i<length)
- {
- if(j==-||s[i]==s[j])
- {
- ++i;
- ++j;
- next[i]=j;
- }
- else
- j=next[j];
- }
- }
- int main()
- {
- int T;
- scanf( "%d", &T );
- while ( T-- )
- {
- scanf( "%s", str );
- len = strlen(str);
- getNext( str, nextval );
- int sub = len - nextval[len];
- if ( sub != len && len % sub == ) puts("");
- else printf( "%d\n", sub - ( nextval[len]%sub ) );
- }
- return ;
- }
HDU 1358 Period
题意:若某串的前i个字符是循环串,输出i以及循环节出现的次数。
做法跟上一题差不多
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
- const int MAXN = ;
- char str[MAXN];
- int nextval[MAXN];
- int len;
- void getNext( char s[], int next[] )
- {
- int length=len;
- int i=,j=-;
- next[]=-;
- while(i<length)
- {
- if(j==-||s[i]==s[j])
- {
- ++i;
- ++j;
- next[i]=j;
- }
- else
- j=next[j];
- }
- }
- int main()
- {
- int cas = ;
- while ( scanf( "%d", &len ), len )
- {
- scanf( "%s", str );
- getNext( str, nextval );
- printf( "Test case #%d\n", ++cas );
- for ( int i = ; i <= len; ++i )
- {
- int sub = i - nextval[i];
- if ( i / sub > && i % sub == )
- {
- printf("%d %d\n", i, i / sub );
- }
- }
- puts("");
- }
- return ;
- }
POJ 2406 Power Strings(求循环串的最大循环周期)
注意这组数据:aabaabaa
显然答案是:1
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- const int MAXN = ;
- char str[MAXN];
- int nextval[MAXN];
- int len;
- void getNext(char s[],int next[])
- {
- int length=len;
- int i=,j=-;
- next[]=-;
- while(i<length)
- {
- if(j==-||s[i]==s[j])
- {
- ++i;
- ++j;
- next[i]=j;
- }
- else
- j=next[j];
- }
- }
- int main()
- {
- while ( scanf( "%s", str ) == && str[] != '.' )
- {
- len = strlen(str);
- getNext( str, nextval );
- int sub = len - nextval[len];
- if ( len % sub == ) printf("%d\n", len / sub );
- else printf("1\n");
- }
- return ;
- }
KMP与循环节相关题目的更多相关文章
- HDU 3746 Cyclic Nacklace (KMP求循环节问题)
<题目链接> 题目大意: 给你一个字符串,要求将字符串的全部字符最少循环2次需要添加的字符数. [>>>kmp next函数 kmp的周期问题] #include &l ...
- KMP解决字符串最小循环节相关问题
经典问题 : 给出一个由某个循环节构成的字符串,要你找出最小的循环节,例如 abababab 最小循环节当是 ab ,而类似 abab 也可以成为它的循环节,但并非最短. 分析 : 对于上述问题有两个 ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
- HDU 1358 Period(KMP+最小循环节)题解
思路: 这里只要注意一点,就是失配值和前后缀匹配值的区别,不懂的可以看看这里,这题因为对子串也要判定,所以用前后缀匹配值,其他的按照最小循环节做 代码: #include<iostream> ...
- HDU 3746 Cyclic Nacklace(KMP+最小循环节)题解
思路: 最小循环节的解释在这里,有人证明了那么就很好计算了 之前对KMP了解不是很深啊,就很容易做错,特别是对fail的理解 注意一下这里getFail的不同含义 代码: #include<io ...
- 用KMP征服循环节问题
以前我还是写过KMP的文章的 现在我们可以求一下循环节啊 Slot Machines Gym - 101667I #include<bits/stdc++.h> using namespa ...
- HDU - 3374 String Problem (kmp求循环节+最大最小表示法)
做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...
- (KMP 根据循环节来计算)Period -- hdu -- 1358
http://acm.hdu.edu.cn/showproblem.php?pid=1358 Period Time Limit: 2000/1000 MS (Java/Others) Memo ...
- ( KMP 求循环节的个数)Power Strings -- poj -- 2406
链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS Memory Limit:65536KB 64bi ...
随机推荐
- 【转】使用webmagic搭建一个简单的爬虫
[转]使用webmagic搭建一个简单的爬虫 刚刚接触爬虫,听说webmagic很不错,于是就了解了一下. webmagic的是一个无须配置.便于二次开发的爬虫框架,它提供简单灵活的API,只需少量代 ...
- cudaMallocPitch()
- 第二天了,由于博主太分心了,看看就跑去研究了一下ssh和ufw以及nmap,现在急需记录一下啦,哈哈!
昨天看到了视频的ssh远程连接,因为我点电脑上装的是一个ubuntu的虚拟机,我根据视频看了一下,自己又试用了一下,我发现自己的ubuntu是能够远程到自己的Mac电脑上,一开始主要是因为自己不能连接 ...
- Oracle SCN与时间的相互转换
1.SCN转换成时间 select scn_to_timestamp(current_scn) from v$database; 2.时间转换成SCN select timestamp_to_scn( ...
- 【iOS】史上最全的iOS持续集成教程 (下)
:first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...
- ES6的数组方法之Array.from
首先说说什么是数组:数组在类型划分上归为Object,属于比较特殊的对象,数组的索引值类似于对象的key值. 数组的几个注意点: 1.数组的长度是可读属性,不可更改,数组的长度根据索引最大值. 2.数 ...
- django+xadmin在线教育平台(九)
django admin介绍 上一章我们进行了需求分析和数据库设计.本章我们来快速搭建一个可用的后台管理系统. 后台管理系统特点: 权限管理 少前端样式.(样式一般不是很看重), 快速开发 djang ...
- spring框架中@PostConstruct的实现原理
在spring项目经常遇到@PostConstruct注解,首先介绍一下它的用途: 被注解的方法,在对象加载完依赖注入后执行. 此注解是在Java EE5规范中加入的,在Servlet生命周期中有一定 ...
- c#字符显示转换{0:d} string.Format()
这一篇实际和前几个月写的没什么本质上的区别.但是这篇更明确一点,学起来easy c#字符显示转换{0:d} C#:String.Format数字格式化输出 : int a = 12345678; // ...
- python--Matplotlib(二)
Matplotlib+pandas作图 一.对csv文件进行提取ruixi.csv 对上述表格进行提取并做图 #-*- coding:utf-8 -*- import matplotlib as mp ...