Power Strings

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 29   Accepted Submission(s) : 14
Problem Description
Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).
 
Input
Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.
 
Output
For each s you should print the largest n such that s = a^n for some string a.
 
Sample Input
abcd aaaa ababab .
 
Sample Output
1 4 3
 
题解:让找串的重复度;

描述:

对于数组s[0~n-1],计算next[0~n](多计算一位)。

考虑next[n],假设t=n-next[n],如果n%t==0,则t就是问题的解,否则解为1。

这样考虑:

比如字符串"abababab",

a  b a b a b a b *

next     -1 0 1 2 3 4 5 6  7

考虑这样的模式匹配,将"abababab#"当做主串,"abababab*"当做模式串,于是进行匹配到n(n=8)时,出现了不匹配:

主串      abababab#

模式串   abababab*

于是模式串需要回溯到next[*]=7,这之前的主串和模式串对应相等,于是需要模式串向右滚动的位移是d=n-next[n]=2,即:

123456789

主串      abababab#

模式串       abababab*

于是可以看出,s[0~1]=s[3~4]=s[5~6]=s[7~8]。

所以位移d=n-next[n]可以看作是构成字符串s的字串(如果n%d==0,存在这样的构成),相应的重复次数也就是n/d。

n-next[n]也就是当前最小匹配的字串长度。。。。。此处的next数组相当于代码中的p数组......

代码:

 #include<stdio.h>
#include<string.h>
const int MAXN=;
int p[MAXN];
char s[MAXN];
int n,len;
void getp(){
int i=,j=-;
p[]=-;
while(i<len){
if(j==-||s[j]==s[i]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
}
int main(){int answer;
while(scanf("%s",s),strcmp(s,".")){
memset(p,,sizeof(p));
len=strlen(s);
getp();
/*for(int i=0;i<=len;i++)printf("%d ",p[i]);
puts("");*/
answer=;
if(len%(len-p[len])==)answer=len/(len-p[len]);
printf("%d\n",answer);
}
return ;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
const int INF=0x3f3f3f3f;
const int MAXN=1000010;
int p[MAXN]; void getp(char* s){
int len=strlen(s);
int i=0,j=-1;
p[0]=-1;
while(i<len){
if(j==-1||s[i]==s[j]){
i++;j++;
p[i]=j;
}
else j=p[j];
}
} int main(){
char s[MAXN];
while(~scanf("%s",s),strcmp(s,".")){
getp(s);
int len=strlen(s);
if(len==0){
puts("0");continue;
}
if(p[len]!=-1&&len%(len-p[len])==0){
printf("%d\n",len/(len-p[len]));
}
else puts("1");
}
return 0;
}

  下面是修正过的:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
typedef long long LL;
const int MAXN = ;
char s[MAXN];
int next[MAXN];
void getp(){
int i = , j = -;
next[i] = j;
while(s[i]){
if(j == - || s[i] == s[j]){
i++;j++;
next[i] = j;
if(s[i] == s[j])
next[i] = next[j];
}
else
j = next[j];
}
} int main(){
while(scanf("%s", s), s[] != '.'){
getp();
int len = strlen(s);
// for(int i = 0; i <= len; i++){
// printf("%d ", next[i]);}puts("");
if(len % (len - next[len]) == && len / (len - next[len]) > ){
printf("%d\n", len / (len - next[len]));
}
else puts("");
}
return ;
}

java写法:

import java.util.Scanner;

public class kmp{
public static class newkmp extends kmpmod{
public void work(char[] str){
getp(str);
//System.out.print(p[len]);
if(len % (len - p[len]) == )
System.out.println(len/(len - p[len]));
else
System.out.println();
}
}
public static char[] str = new char[];
public static void main(String[] argv){
Scanner cin = new Scanner(System.in);
newkmp kmp1 = new newkmp();
while(cin.hasNext()){
str = cin.next().toCharArray();
if(str.length == && str[] == '.')
break;
//System.out.print(str);
kmp1.work(str);
}
}
}
abstract class kmpmod{
protected int p[] = new int[];
protected int len;
public void getp(char[] str){
for(int i = ; i < p.length; i++){
p[i] = ;
}
len = str.length;
int i = , j = -;
p[] = -;
while (i < str.length) {
if(j == - || str[i] == str[j]){
i++; j++;
p[i] = j;
}
else{
j = p[j];
}
}
}
public abstract void work(char[] str);
}

Power Strings(kmp妙解)的更多相关文章

  1. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  2. poj 2406 Power Strings kmp算法

    点击打开链接 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 27368   Accepted:  ...

  3. UVA - 10298 Power Strings (KMP求字符串循环节)

    Description Problem D: Power Strings Given two strings a and b we define a*b to be their concatenati ...

  4. UVA10298 Power Strings [KMP]

    题目传送门 Power Strings 格式难调,题面就不放了. 一句话题意,求给定的若干字符串的最短循环节循环次数. 输入样例#1: abcd aaaa ababab . 输出样例#1: 1 4 3 ...

  5. POJ2406 Power Strings —— KMP or 后缀数组 最小循环节

    题目链接:https://vjudge.net/problem/POJ-2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Tot ...

  6. poj2406 Power Strings (kmp 求最小循环字串)

    Power Strings   Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 ...

  7. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  8. poj 2406 Power Strings(KMP变形)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28102   Accepted: 11755 D ...

  9. POJ2406 Power Strings KMP算法

    给你一个串s,如果能找到一个子串a,连接n次变成它,就把这个串称为power string,即a^n=s,求最大的n. 用KMP来想,如果存在的话,那么我每次f[i]的时候退的步数应该是一样多的  譬 ...

随机推荐

  1. UESTC_贪吃蛇 CDOJ 709

    相信大家都玩过贪吃蛇游戏吧. 在n×m的迷宫中,有着一条长度不超过9的贪吃蛇,它已经将所有的食物吃光了,现在的目标是移动到出口. 它走的时候不能碰到自己的身体,也不能碰到墙壁.(如果贪吃蛇的长度> ...

  2. 剑指offer-面试题1:赋值运算符函数

    题目:如下为类型CMyString的声明,请为该类型添加赋值运算符函数 class CMyString { public: CMyString(char *pData=NULL); CMyString ...

  3. LeeCode-Remove Linked List Elements

    Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --& ...

  4. nginx编译配置

    1, 正向代理是一个位于内网客户端和外网原始服务器之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标,然后由代理服务器向 原始服务器转交请求并将获得的内容返回给客户端.正向代理 ...

  5. mongoDb c driver

    1,yum dependencies Centos,RHEL Fedora: $ sudo yum install git gcc automake autoconf libtool Debian: ...

  6. PHP 表单处理

    PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据(form-data). PHP - 一个简单的 HTML 表单 下面的例子显示了一个简单的 HTML 表单,它包含两个输入字段和一个 ...

  7. UGUI 滚动视图

    滚动视图是常用的UI控件之一,它是由多个基本控件组合而成.如图 ==================================================================== ...

  8. .Net语言中关于AOP 的实现详解

    来源: IT人家  发布时间: 2011-03-22 20:28  阅读: 3546 次  推荐: 2   原文链接   [收藏] 摘要:该文章主要和大家讲解开发应用系统时在.Net语言中关于AOP ...

  9. 执行此安装程序之前,必须安装 32 位 Windows 映像处理组件(WIC)解决的方法

    我们在Windows Service 2003上安装 Microsoft .NET Framework4.0时常常出现以下的报错 执行此安装程序之前,必须安装 32 位 Windows 映像处理组件( ...

  10. H5单页面架构:backbone + requirejs + zepto + underscore

    首先,来看看整个项目结构. 跟上一篇angular类似,libs里多了underscore和zepto.三个根目录文件: index.html:唯一的html main.js:requirejs的配置 ...