POJ 2406 Power Strings

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.

题意

定义*为字符串ab*ab 得到字符串abab,同理ab^n得到的字符串为abab……(n个ab)。给定长度不超过100,0000的字符串s,设这个字符串的子字符串为a,求a^n=s的n的最大值。

解析

题意相当于:一个字符串s的最小能通过复制构成它的子字符串是a,问这个字符串由多少个a构成,因为大字符串s是由最小子字符串a复制而来,因此s的长度是a的倍数,s.length()肯定整除a.length().s是输入,长度已知,因此我们只要找到能被s.length整除的i,s的前i个就当作a,判断这个s能否由a复制得来,因为要最小,所以i递增。
(题目源于网络)

#include<iostream>
#include<math.h>
#include <algorithm>
#include<stdio.h>
#include<string.h>
#include<stack> 
#include<queue>
using namespace std;
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define req(i, a, b) for(int i=(a); i<=(b); i++)
#define ull unsigned __int64
#define sc(t) scanf("%d",&(t))
#define sc2(t,x) scanf("%d%d",&(t),&(x))
#define pr(t) printf("%d\n",(t))
#define pf printf
#define prk printf("\n")
#define pi acos(-1.0)
#define ms(a,b) memset((a),(b),sizeof((a)))
#define mc(a,b) memcpy((a),(b),sizeof((a))) 
#define w while
typedef long long ll;
typedef vector<int> vi;

char s[1000005];
int ans;

int main()
{
    int len, i, f, j;
    bool flag, vis;
    w(scanf("%s",&s) && s[0]!='.')
    {
        flag = 0;//标记是否有最小子字符串
        len = strlen(s);
        if(len == 1)//情形1:长度为1
        {
            pf("1\n");
            continue;
        }
        i = 1;
        w(s[i] == s[0])//情形2:字符串s是由单个字符构成
        i++;
        if(i == len)
        {
            pr(len);
            continue;
        }
        if(len == 2 || len == 3) //情形3:排除了情形2的单字符可能
        {
            pf("1\n");
            continue;
        }
        /*情形4,wa了很多次,
        因为我一直定义i<=sret(len),
        用平方根是计算有没有因子,本题只要是倍数就满足*/
        for(i=2; i<=len/2; i++)
        {
            if(len % i == 0)
            {
                vis = 0;//标记是否是可复制子字符串
                for(j=i; j<len; j++)
                {
                    rep(k, 0, i)
                    {
                        if(s[j]!=s[k])
                        {
                            vis = 1;//说明这个字符串不能由它复制
                            break;
                        }
                        j++;
                    }
                    if(vis)
                    break;
                    j--;
                }
             }
             if(j == len)
             {
                flag = 1;
                break;
              } 
        }
        if(flag)
        pr(len / i);
        else pf("1\n");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值