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. Jquery时间快捷控件(Jtime)配置v1.1

    1.插件代码 /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * @date 2013-0 ...

  2. Pasha and String(思维,技巧)

    Pasha and String Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u S ...

  3. ZOJ1372 POJ 1287 Networking 网络设计 Kruskal算法

    题目链接:problemCode=1372">ZOJ1372 POJ 1287 Networking 网络设计 Networking Time Limit: 2 Seconds     ...

  4. Esper学习之六:EPL语法(二)

    中秋三天,说闲也不闲,调调工作的代码,倒还解决不少问题.不过也是因为最近工作忙的缘故,Esper被我冷落不少日子了,趁着今天最后一天,赶紧写一篇出来. 从上一篇开始说EPL的语法,主要是关于注解的.今 ...

  5. Android系统Surface机制的SurfaceFlinger服务的启动过程分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8022957 在前面一篇文章中,我们简要介绍了A ...

  6. QT笔记(1)--QT编程环境搭建

    一.QT简介 Qt  是一个1991年由奇趣科技开发的跨平台C++图形用户界面应用程序开发框架.它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器.Qt是面向对象的框架,使用特殊 ...

  7. error: ‘for’ loop initial declarations are only allowed in

    使用gcc,出现如下错误: thread_join.c:7:5: error: 'for' loop initial declarations are only allowed in C99 mode ...

  8. 设置grub密码

    一,明文加密的方法 vi /etc/grub.conf 在hiddenmenu下添加password=1234,保存退出. 二,密文加密的方法 2.1, 使用SHA加密方式.grub-crypt  回 ...

  9. Identity 验证,Authorize 特性

    多类型角色访问 //[Authorize] //[Authorize(Roles = "User")] //[Authorize(Roles="Administrator ...

  10. MVC 过滤器 ActionFilterAttribute

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...