Power Strings POJ - 2406(next水的一發 || 尾碼數組)

來源:互聯網
上載者:User

標籤:bsp   ace   尾碼數組   color   最長公用首碼   freopen   重複   sign   code   

  尾碼數組專題的 emm。。

就next 迴圈節。。/

有尾碼數組也可以做

從小到大枚舉長度i,如果長度i的子串剛好是重複了len/i次,應該滿足len % i == 0和rank[0] - rank[i] == 1(整個串的等級比 i位置開始的尾碼的等級大1  (i位置開始的尾碼即為比總串低一個等級的尾碼)) 和height[rank[0]] == len-i (整個串 和 比它低一個等級的串的最長公用首碼的長度 是總長度減去這個迴圈節的長度)這些條件的

 

#include <iostream>#include <cstdio>#include <sstream>#include <cstring>#include <map>#include <cctype>#include <set>#include <vector>#include <stack>#include <queue>#include <algorithm>#include <cmath>#define rap(i, a, n) for(int i=a; i<=n; i++)#define rep(i, a, n) for(int i=a; i<n; i++)#define lap(i, a, n) for(int i=n; i>=a; i--)#define lep(i, a, n) for(int i=n; i>a; i--)#define rd(a) scanf("%d", &a)#define rlld(a) scanf("%lld", &a)#define rc(a) scanf("%c", &a)#define rs(a) scanf("%s", a)#define MOD 2018#define LL long long#define ULL unsigned long long#define Pair pair<int, int>#define mem(a, b) memset(a, b, sizeof(a))#define _  ios_base::sync_with_stdio(0),cin.tie(0)//freopen("1.txt", "r", stdin);using namespace std;const int maxn = 1e6+10, INF = 0x7fffffff;int nex[maxn];void get_next(char *s){    int len = strlen(s);    int j, k;    j = 0, k = -1, nex[0] = -1;    while(j < len)    {        if(k == -1 || s[j] == s[k])            nex[++j] = ++k;        else            k = nex[k];    }}char s[maxn];int main(){    while(~rs(s) && s[0] != ‘.‘)    {        get_next(s);        int n = strlen(s);        if(n % (n - nex[n]) == 0)            cout<< n / (n - nex[n]) <<endl;        else            cout<< 1 <<endl;    }    return 0;}

 

Power Strings POJ - 2406(next水的一發 || 尾碼數組)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.