poj2406 Power Strings 2012-01-11

http://162.105.81.212/JudgeOnline/problem?id=2406

____________________________

求最长重复字串。即将字符串自我匹配,然后字符串长度减去最后一位的next值即为最长重复字串的长度。注意他求的是由多少个最长重复字串组成。

____________________________

 1 Program stone;
 2 var i,j:longint;
 3     s:ansistring;
 4     b:array[1..1000005]of longint;
 5 Begin
 6  assign(input,‘input.in‘);reset(input);
 7   readln(s);
 8   while s<>‘.‘ do
 9    begin
10      j:=0;
11      b[1]:=0;
12      for i:=2 to length(s) do
13       begin
14         while (j>0)and(s[i]<>s[j+1]) do j:=b[j];
15         if s[j+1]=s[i] then inc(j);
16         b[i]:=j;
17       end;
18      i:=length(s);
19      if i mod (i-b[i])=0 then writeln(i div(i-b[i])) else writeln(1);
20      readln(s);
21    end;
22 end.
23
24  
时间: 2024-08-24 20:01:11

poj2406 Power Strings 2012-01-11的相关文章

POJ2406 Power Strings 【KMP】

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 31388   Accepted: 13074 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 = "

poj2406 Power Strings (KMP)

这题跟HDU 1358 Period (KMP)差不多,稍微修改代码就行了. 关于KMP的更多知识,请关注从头到尾彻底理解KMP(2014年8月4日版). #include<stdio.h> #include<string.h> int n,next[1000000]; char p[1000000]; void getnext() { int k=0,j=1; next[0]=-1;next[1]=0; while (j<n) { if (k==-1||p[j]==p[k]

poj2406 Power Strings

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33273   Accepted: 13825 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 = "

poj2406 Power Strings(kmp失配函数)

Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 39291 Accepted: 16315 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 = "abcd

POJ2406:Power Strings(后缀数组DC3)

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 inte

POJ2406 Power Strings(KMP,后缀数组)

这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即为结果. 若lcp(suffix(0), suffix(k))的长度为n- k,则将串每k位分成一段,则第1段与第2段可匹配,又可推得第2段与第3段可匹配……一直递归下去,可知每k位都是相同的,画图可看出匹配过程类似于蛇形. 用倍增算法超时,用dc3算法2.5秒勉强过. #include<cstdi

POJ2406 Power Strings【KMP】

题目链接: http://poj.org/problem?id=2406 题目大意: 给定两个字符串a和b,定义a*b为两个字符串的链接.比如,a = "abc",b = "def",则 a*b ="abcdef".这个定义当作是多项式.则一个字符串的非负整数次幂可定义如下: a^0 = "",a^(n+1) = a*a^n. 现在给你一个字符串s,求出最大的n,满足s = a^n(a为s的某个子串).比如s = "

POJ2406 Power Strings next数组应用

题目描述: 给定一个字符串,求其最大循环次数(即求最小循环节长度) 输入样例 abcd ababab aaaa . 输出样例 1 3 4 解题思路: KMP算法中next数组的应用. len-next[len]表示的是字符串相同前缀空出来的一段,由next数组性质可知,这一段可以不断向前推出相等,所以只要判断len是否可以整除len-next[len]就可以了.否则就是1. 代码如下 #include <cstdio> #include <cstring> const int ma

【后缀数组】poj2406 Power Strings

连续重复子串(pku2406)给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值.算法分析:做法比较简单,穷举字符串 S 的长度 k,然后判断是否满足.判断的时候,先看字符串 L 的长度能否被 k 整除,再看 suffix(1)和 suffix(k+1)的最长公共前缀是否等于 n-k.在询问最长公共前缀的时候, suffix(1)是固定的,所以 RMQ问题没有必要做所有的预处理,只需求出 height 数组中的每一个数到height[rank[1]]之间