Power Strings POJ - 2406,字符串hash

题目链接:POJ - 2406

题目描述

定义两个字符串s1和s2的乘积s1*s2为将s1和s2连结起来得到的字符串。 
例如:s1="xy",s2="z",那么s1*s2="xyz"。 
由此可以定义s1的幂次:s1^0="",s1^n=s1*s1^(n-1),n>0。

输入

输入包含多组测试数据。 
每组数据由一行构成,包含一个字符串s。 
输入数据以"."结束。

输出

对于每组输入数据输出一行,找出最大的正整数n,使得存在某个字符串a,s = a^n.

样例输入

abcd
aaaa
ababab
.

样例输出

1
4
3题解:字符串hash,然后枚举子串长度,。模板套一下就可以
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define rank wepsdas
#define PI 3.14159265
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define eps 1e-7
using namespace std;
const int base=23;
const int N=1e6+100;
const int mod=1e9+7;
struct hash
{
    char s[N];
    ull h[N],qp[N];
    ll p[N],len,val[N];
    void init()
    {
        h[0]=val[0]=0;
        qp[0]=p[0]=1;
        len=strlen(s+1);
        for(int i=1;i<=len;i++)
        {
            qp[i]=qp[i-1]*base;
            h[i]=h[i-1]*base+s[i];
        }
    }
    ull get_hash(int l,int r)
    {
        return h[r]-h[l-1]*qp[r-l+1];
    }
}ac;
bool judge(int l)
{
    ull tmp=ac.get_hash(1,l);
    for(int i=l+1;i<=ac.len;i+=l)
    {
        if(tmp!=ac.get_hash(i,i+l-1))return false;
    }
    return true;
}
int main() {

    while(scanf("%s",ac.s+1))
    {
       ac.init();
       if(ac.len==1&&ac.s[1]==‘.‘)break;
      // printf("%s",ac.s+1);
   //?    for(int i=)
       int ans=-1;
       for(int i=1;i<=ac.len;i++)
       {
            if(ac.len%i==0)
            {
                if(judge(i))
                {
                   ans=i;break;
                }

            }
       }
       printf("%d\n",ac.len/ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/lhclqslove/p/10322531.html

时间: 2024-10-25 05:57:58

Power Strings POJ - 2406,字符串hash的相关文章

Power Strings POJ - 2406

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

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: 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

G - Power Strings POJ 2406 (字符串的周期)

G - 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

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

链接: http://poj.org/problem?id=2406 Power Strings Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc&qu

Power Strings (poj 2406 KMP)

Language: Default Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33205   Accepted: 13804 Description Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def"

Power Strings POJ 2406【KMP Next的应用】

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 inte

Power Strings POJ - 2406 后缀数组

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 defin

Power Strings - POJ 2406(求循环节)

题目大意:叙述的比较高大上,其实就是一个字符串B = AAAAAAA,求出来这个A最短有多长   分析:注意如果这个串不是完全循环的,那么循环节就是就是它本身.   代码如下: #include<stdio.h> #include<string.h> const int MAXN = 1e6+7; const int oo = 1e9+7; char s[MAXN]; int next[MAXN]; void GetNext(int N) { int i=0, j=-1; next

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

后缀数组专题的 emm.. 就next 循环节../ 有后缀数组也可以做 从小到大枚举长度i,如果长度i的子串刚好是重复了len/i次,应该满足len % i == 0和rank[0] - rank[i] == 1(整个串的等级比 i位置开始的后缀的等级大1  (i位置开始的后缀即为比总串低一个等级的后缀)) 和height[rank[0]] == len-i (整个串 和 比它低一个等级的串的最长公共前缀的长度 是总长度减去这个循环节的长度)这些条件的 #include <iostream>