B. Power Strings

Time Limit: 3000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

 
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.
 
解题:求字符串的循环节长度。利用KMP的适配数组。如果字符长度可以被(字符长度-fail[字符长度])整除,循环节这是这个商,否则循环节长度为1,即就是这个字符本身。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = ;
char str[maxn];
int fail[maxn];
void getFail(int &len) {
int i,j;
len = strlen(str);
fail[] = fail[];
for(i = ; i < len; i++) {
j = fail[i];
while(j && str[j] != str[i]) j = fail[j];
fail[i+] = str[j] == str[i] ? j+:;
}
}
int main() {
int len;
while(gets(str) && str[] != '.') {
getFail(len);
if(len%(len-fail[len])) puts("");
else printf("%d\n",len/(len-fail[len]));
}
return ;
}

xtu字符串 B. Power Strings的更多相关文章

  1. poj 2406 Power Strings【字符串+最小循环节的个数】

                                                                                                      Po ...

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

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

  3. POJ 2406 Power Strings (KMP)

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

  4. poj 2406:Power Strings(KMP算法,next[]数组的理解)

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30069   Accepted: 12553 D ...

  5. POJ 2406:Power Strings

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 41252   Accepted: 17152 D ...

  6. E - Power Strings,求最小周期串

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

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

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

  8. 【POJ2406】 Power Strings (KMP)

    Power Strings Description Given two strings a and b we define a*b to be their concatenation. For exa ...

  9. Power Strings

    Power Strings TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Totalsubmit: 1791   Accepted: 528 Descr ...

随机推荐

  1. oracle 查看未关闭连接

    查看连接状态.问题电脑等信息: select sid,serial#,username,program,machine,status from  v$session; 2.查看sql; select ...

  2. Netbeans自定义折叠代码

    只需要在模块开始注释以//<editor-fold>开始, 在模块结束行以 //</editor-fold>结束即可 Can I Create Custom Code Fold ...

  3. Android开发-浅谈架构(一)

    写在前面的话 嗯 聊聊架构. 这段时间一直在维护旧项目. 包括自己之前写的新项目 越来越发现 一个架构清晰的项目往往让人赏心悦目.不至于在一个bug丢过来之后手足无措.包括以后别人接收自己的项目 能很 ...

  4. 获取一段HTML文本中的第一张图片与截取内容摘要

    有时候我们获得到的数据是一段HTML文本,也许这段文本里面有许多图片,需要截取一张作为标题图片,这时就可以用到下面这个方法获取到第一张图片: #region 获取第一张图片 /// <summa ...

  5. wp跳转到评价界面代码

    wp跳转到评价界面代码(仅适用于wp8.0) MarketplaceReviewTask marketplaceReviewTask = new MarketplaceReviewTask(); ma ...

  6. AJPFX总结java开发常用类(包装,数字处理集合等)(二)

    二:进军集合类 集合其实就是存放对象的容器,专业点说就是集合是用来存储和管理其他对象的对象,即对象的容器.集合可以扩容,长度可变,可以存储多种类型的数据,而数组长度不可变,只能存储单一类型的元素 用一 ...

  7. 8.bootstrap下拉菜单、按钮组、按钮式下拉菜单

    下拉菜单 dropdown 对齐方式: .dropdown-menu-right     .dropdown-menu-left <div class="container" ...

  8. 在js中怎样获得checkbox里选中的多个值?(jQuery)

    思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中,false表示未选中).下面进行实例演示: 1.HTML结构 <input type=&qu ...

  9. RHEL6.4上Samba/NFS服务器简单配置

    近期在RHEL6.4上尝试搭建一个NAS,底层使用XFS文件系统,对外主要提供samba协议和NFS协议共享,这里把主要步骤记录下来. 环境:RHEL6.4,IP:192.168.50.117 1.关 ...

  10. MFC技术积累——基于MFC对话框类的那些事儿2

    3. 绘图 3.1 对话框资源编辑 首先通过添加控件的方式来创建一个简单的绘图对话框如图所示,创建步骤为: 第一.在VC++6.0软件环境的灰色空白区域右击,选中Controls,然后会弹出一个控件对 ...