zoukankan      html  css  js  c++  java
  • UVA 1328

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131

    题意:给出一个长度为n的字符串,要求找到一些i,满足说从1~i为多个个的重复子串构成,并输出子串的个数。

    题解:对kmp中预处理的数组的理解   

    //作者:1085422276
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include<bits/stdc++.h>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    const int inf = 10000000;
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    ll exgcd(ll a,ll b,ll &x,ll &y)
    {
        ll temp,p;
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        p=exgcd(b,a%b,x,y);
        temp=x;
        x=y;
        y=temp-(a/b)*y;
        return p;
    }
    //*******************************
    int n,p[1000001];char a[1000001];
    int main()
    {
        int oo=1;
        while(scanf("%d",&n)!=EOF)
        {
            if(n==0)break;
            scanf("%s",a+1);
            memset(p,0,sizeof(p));
            int j=0;
            for(int i=2;i<=n;i++)
            {
                while(j>0&&a[j+1]!=a[i])j=p[j];
                if(a[j+1]==a[i])j++;
                p[i]=j;
            }
            cout<<"Test case #"<<oo++<<endl;
            for(int i=2;i<=n;i++)
            {
                if(p[i]>0&&i%(i-p[i])==0){
                    cout<<i<<" "<<i/(i-p[i])<<endl;
                }
            }
            cout<<endl;
        }
        return 0;
    }
    代码
  • 相关阅读:
    sql查询
    PHP常用的设计模式
    PHP内存管理和垃圾回收机制
    记一次面试
    获取py文件函数名及动态调用
    正确解决 mysql 导出文件 分隔符 问题
    解决ValueError: cannot convert float NaN to integer
    Python ---接口返回值中文编码问题
    pandas python 读取大文件
    【neo4J】后台关闭后,前端还能打开视图
  • 原文地址:https://www.cnblogs.com/zxhl/p/4772162.html
Copyright © 2011-2022 走看看