poj2406 Power Strings(kmp)
poj2406 Power Strings(kmp)
给出一个字符串,问这个字符串是一个字符串重复几次。要求最大化重复次数。
若当前字符串为S,用kmp匹配'\0'+S和S即可。
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=2e6+5;
char s1[maxn], s2[maxn];
int n1, n2, nxt[maxn], ans;
int main(){
while (~scanf("%s", s1)){
if (s1[0]=='.') break;
memcpy(s2, s1, sizeof(s1));
n1=n2=strlen(s1);
for (int i=n1; i<n1*2; ++i) s1[i]=s1[i-n1];
n1*=2;
int j=nxt[0]=-1; //j:i-1的border位置
for (int i=1; i<n2; ++i){
for (; ~j&&s2[j+1]!=s2[i]; j=nxt[j]); ++j;
if (!j&&s2[0]!=s2[i]) j=-1;
nxt[i]=j;
} j=-1; //j:s2中已经匹配完成的位置
for (int i=1; i<n1; ++i){
for (; ~j&&s2[j+1]!=s1[i]; j=nxt[j]); ++j;
if (j==n2-1){ ans=i-n2+1; break; }
if (!j&&s2[0]!=s1[i]) j=-1;
}
printf("%d\n", n2/ans);
}
return 0;
}
poj2406 Power Strings(kmp)的更多相关文章
- poj2406 Power Strings(kmp失配函数)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 Descr ...
- 【POJ2406】 Power Strings (KMP)
Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...
- POJ 2406 Power Strings(KMP)
Description Given two strings a and b we define a*b to be their concatenation. For example, if a = & ...
- Power Strings(KMP)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 45008 Accepted: 18794 D ...
- poj 2406:Power Strings(KMP算法,next[]数组的理解)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30069 Accepted: 12553 D ...
- poj 2406 Power Strings(KMP入门,next函数理解)
Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 37685 Accepted: 15590 D ...
- 「UVA10298」 Power Strings(KMP
题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 复制 abcd aaaa ababab . 输出样例#1: 复制 1 4 3 题解 Luogu的题解 这里是对目前 ...
- 【POJ2406】Power Strings(KMP,后缀数组)
题意: n<=1000000,cas较大 思路:这是一道论文题 后缀数组已弃疗,强行需要DC3构造,懒得(不会)写 ..]of longint; n,m,i,j,len,ans,st:longi ...
- poj 2406 Power Strings(kmp求一个串的重复子串)
题意:重复子串次数 思路:kmp #include<iostream> #include<stdio.h> #include<string.h> using nam ...
随机推荐
- (转)SqlDependency学习笔记
本文转载自:http://www.cnblogs.com/yjmyzz/archive/2009/06/14/1502921.html sqlDependency提供了这样一种能力:当被监测的数据库中 ...
- minidump-DMP文件的生成和使用
转载地址点击打开链接 1.生成dmp的程序 #include <dbghelp.h> #pragma comment(lib, "dbghelp.lib") //设 ...
- 最新版CocoaPods的安装流程
1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sources -a https://ruby.taoba ...
- c语言-树的基础知识
第一.树的定义: 1.有且只有一个称为根的节点 2.有若干个互不相交的子树,这些子树本身也是一颗树 第二.专业术语: 树的深度:从根节点到最低层,节点的层数 ,称之为树的深度. 根节点是第一 ...
- Celery-4.1 用户指南: Concurrency (并发)
简介 Eventlet 的主页对它进行了描述:它是一个python的并发网络库,可以让你更改如何运行你的代码而不是怎么编写代码. 对高可扩展非阻塞IO操作,它使用 epoll或者libevent. C ...
- Git中远程仓库的使用
1.查看当前的远程库 要查看当前配置有哪些远程仓库,可以用 git remote 命令,它会列出每个远程库的简短名字.在克隆完某个项目后,至少可以看到一个名为 origin 的远程库,Git 默认使用 ...
- spring整合web项目演示
- 根据/proc/meminfo对空闲内存进行占用
#include <stdio.h> #include <sys/sysinfo.h> #include <linux/kernel.h> /* 包含sysinfo ...
- vue 生命周期小结
生命周期小结生命周期钩子的一些使用方法: beforecreate : 可以在这加个loading事件,在加载实例时触发 created : 初始化完成时的事件写在这里,如在这结束loading事件, ...
- SqlServer中把结果集放到到临时表的方法(转)
一. SELECT INTO 1. 使用select into会自动生成临时表,不需要事先创建 select * into #temp from sysobjects 01. 把存储过程结 ...