POJ 2406 Power Strings

这是一个关于POJ 2406题目的解题报告,主要涉及字符串处理。程序中定义了一个名为`getnext`的函数用于获取字符串的next数组,并在`main`函数中读取输入字符串,计算满足条件的子串数量并输出结果。
摘要由CSDN通过智能技术生成

Power Strings

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

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.

来源

 

示例程序

 
  1. #include<stdio.h>  
  2.     #include<string.h>  
  3.     #define h 1000001  
  4.     char str1[h];  
  5.     int next[h];  
  6.     void getnext(char str[h])  
  7.     {  
  8.         int i  = 0;  
  9.         int len = strlen(str);  
  10.         next[0] = -1;  
  11.         int j = -1;  
  12.         while(i < len)  
  13.         {  
  14.             if( j == -1 || str[i] == str[j])  
  15.             {  
  16.                 ++i;  
  17.                 ++j;  
  18.                 if( str[i]!= str[j])  
  19.                     next[i] = j;  
  20.                 else  
  21.                     next[i] = next[j];  
  22.             }  
  23.             else  
  24.                 j = next[j];  
  25.         }  
  26.     }  
  27.     int main()  
  28.     {  
  29.             int n,i,len;  
  30.             while(scanf("%s",str1)!=EOF && str1[0]!='.')  
  31.             {  
  32.                 getnext(str1);  
  33.                 len = strlen(str1);  
  34.                 if(len%(i=len-next[len])==0)   
  35.                 {  
  36.                   n=len/i;  
  37.                 }  
  38.                   else   
  39.                       n=1;    
  40.                      printf("%d\n",n);   
  41.             }  
  42.             return 0;  
  43.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶孤心丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值