UVA 1328 - Period KMP

來源:互聯網
上載者:User

標籤:

題目連結: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;}
代碼

 

UVA 1328 - Period KMP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.