Power Strings

POJ - 2406
时限: 3000MS   内存: 65536KB   64位IO格式: %I64d & %I64u

提交 状态

已开启划词翻译

问题描述

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).

输入

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.

输出

For each s you should print the largest n such that s = a^n for some string a.

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3

提示

This problem has huge input, use scanf instead of cin to avoid time limit exceed.

来源

题意:一个字符串可以由其循环节循环n次得到,问n最大可以是多少……
kmp练习。
 #include <iostream>
#include <cstdio>
#include <cstring> using namespace std; #define maxn 10000008 char s[maxn];
int next[maxn]; void getNext()
{
int j, k, i;
i = strlen(s); j = ;
k = -;
next[] = -;
while(j < i)
{
if(k == - || s[j] == s[k])
{
j++;
k++;
next[j] = k;
}
else
k = next[k];
}
} int main()
{
while()
{
scanf("%s", s);
if(s[] == '.')
break; int q = strlen(s); memset(next, , sizeof(next));
getNext(); int k = next[q]; // k表示s串 q位置的最长的前缀和后缀相等的长度。q-k代表最小循环节的长度 if(q % (q - k) == ) // q-k就是最小循环节的长度
printf("%d\n", q / (q - k));
else
printf("1\n");
}
return ;
}

Power Strings POJ - 2406的更多相关文章

  1. ( KMP 求循环节的个数)Power Strings -- poj -- 2406

    链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bi ...

  2. Power Strings (poj 2406 KMP)

    Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   ...

  3. Power Strings POJ - 2406 后缀数组

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&quo ...

  4. Match:Power Strings(POJ 2406)

     字符串前缀的阶 题目大意:求前缀的阶 和POJ1961是一样的,KMP的Next数组的应用,不要用STL,不要一个一个读入字符(IO永远是最慢的) #include <iostream> ...

  5. Power Strings - POJ 2406(求循环节)

    题目大意:叙述的比较高大上,其实就是一个字符串B = AAAAAAA,求出来这个A最短有多长   分析:注意如果这个串不是完全循环的,那么循环节就是就是它本身.   代码如下: #include< ...

  6. Power Strings POJ - 2406(next水的一发 || 后缀数组)

    后缀数组专题的 emm.. 就next 循环节../ 有后缀数组也可以做 从小到大枚举长度i,如果长度i的子串刚好是重复了len/i次,应该满足len % i == 0和rank[0] - rank[ ...

  7. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  8. POJ 2406 Power Strings

    F - Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u S ...

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

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

随机推荐

  1. linux(centos6.5)常用命令

    前言:由于项目项目使用的是linux服务器,因此会使用到较多linux命令,本文对centos下常用命令进行记录 1.vi的三种模式 2.解压缩相关 3.用户相关 4.文件相关 5.各种查看命令 1. ...

  2. vi, Java, Ant, Junit自学报告 - 实训week1

    vi, Java, Ant, Junit自学报告 2017软件工程实训 15331023 陈康怡 vi Vi是linux系统的标准文本编辑器,采用指令的方式进行操作,此处仅记录部分常用的指令. vi模 ...

  3. 【ABAP系列】SAP ABAP ALV合计或者小计 添加自定义文本

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP ALV合计或者小计 ...

  4. TensorFlow学习笔记4-线性代数基础

    TensorFlow学习笔记4-线性代数基础 本笔记内容为"AI深度学习".内容主要参考<Deep Learning>中文版. \(X\)表示训练集的设计矩阵,其大小为 ...

  5. Java中StringHelp

    import java.util.Collection; import java.util.Map; import java.util.UUID; public class StringHelper ...

  6. 《深入浅出WPF》学习总结之学前知识

    一个WPF应用的组成结构 Properties:存放程序资源(图标.图片.静态字符串等) References:标记了当前项目需要引用哪些其他项目App.xmal:程序的主体.在Windows系统里, ...

  7. leveldb memtable

    memtable常驻于内存,需要按照key进行排序,通常意义上的话,可以使用二叉查找树来实现,跟进一步可以使用红黑树保证树的平衡,但是leveldb中使用了另外的一种数据结构:跳表Skip List. ...

  8. 396. Coins in a Line III

    刷 July-31-2019 换成只能从左边或者右边拿.这个确实和Coins in a Line II有关系. 和上面思路一致,也是MinMax思路,只不过是从左边和右边选,相应对方也是这样. pub ...

  9. iptables防火墙常用命令

    iptables防火墙启动停止和基本操作 iptables是centos7之前常用的防火墙,在centos7上使用了firewall 防火墙基本操作: # 查询防火墙状态 service iptabl ...

  10. hibernate的get方法和load方法区别

    读者需注意:Hibernate版本不同,运行机制不太一样,以下是hibernate3.x作为讲解 get方法: Hibernate会确认一下该id对应的数据是否存在,首先在session缓存中查找,然 ...