poj------2406 Power Strings
A - Power Strings 难度:☆☆
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 2406
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
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceed.
from http://poj.org/problem?id=2406
考察kmp算法中的next数组....
next的两种表示方法,第一种是前缀next[]数组...
while(i<len)
{
if(-1==j||ss[i]==ss[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
另种表示方法文为:
while(i<len)
{
if(j==-1||ss[i]==s[j])
{
i++:
j++;
if(ss[i]==ss[j])
{
next[i]=next[j];
}
else next[i]=j;
}
else j=next[j];
}
这道题是考察有多少个重复的最大n所以我们不妨看他的回溯长度len_D=len(已经匹配的位置) --next[len](对应部分的匹配值);
Java代码:
1 //package dek0;
2
3 import java.util.Scanner;
4
5 public class Main {
6
7 public static void main(String args[])
8 {
9 Scanner reader = new Scanner(System.in);
10 String ss="";
11 while(reader.hasNext())
12 {
13 ss=reader.next();
14 if(ss.charAt(0)=='.') break;
15 int len=ss.length();
16 int next[]=new int [len+1];
17 next[0]=-1;
18 int i=0,j=-1;
19 while(i<len)
20 {
21 if(j==-1||ss.charAt(i)==ss.charAt(j))
22 {
23 i++;
24 j++;
25 /* if(ss.charAt(i)==ss.charAt(j))
26 next[i]=next[j];
27 else next[i]=j;*/
28 next[i]=j;
29 }
30 else j=next[j];
31 }
32 if(len%(len-next[len])==0)
33 System.out.println(len/(len-next[len]));
34 else
35 System.out.println(1);
36 }
37 }
38 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Scrapy框架: 通用爬虫之CrawlSpider
- Scrapy框架: 通用爬虫之XMLFeedSpider
- Scrapy框架: 通用爬虫之CSVFeedSpider
- Scrapy框架: 通用爬虫之SitemapSpider
- Scrapy框架: 异常错误处理
- Scrapy框架: Request回调函数
- Python快速设置Excel表格边框
- SwiftUI:contextMenu 菜单
- [已解决]报错: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/User
- Python建立pip.ini
- 使用requests_html抓取数据
- AkShare-能源数据-碳排放-深圳
- ClickHouse入门实例-样例数据(ontime)
- Docker的简单使用
- AkShare-中国宏观-外汇和黄金