POJ 1509 Glass Beads(字串的最小標記法)

來源:互聯網
上載者:User

題目連結:http://poj.org/problem?id=1509

這個題目開始以為是尾碼數組,拼接同一個串然後求排名,這樣來計算

也沒想到還有這麼一個演算法,可以直接求出字串的最小表示方法,學習了,不過理解還不是很深刻

這個演算法實現起來比較簡單,如果理解為就是這樣,記住就行了,那還是比較好理解的

但是如果真正想糾結於具體細節等還是比較繞人的!

#include <iostream>#include <string.h>#include <stdio.h>using namespace std;#define maxn 210000char str[maxn];int n;int get_min(char *name,int len){    int i=0,j=1,k=0;    int t;    while(i<len && j<len && k<len)    {        t=name[i+k < len ? i+k : i+k-len]-name[j+k < len ? j+k : j+k-len];        if(t==0)        k++;        else        {            if(t>0)            i=i+k+1;            else            j=j+k+1;            if(i==j)//如果後面的加k那麼就可能出現這種情況            j++;            k=0;        }    }    return i < j ? i: j;}int main(){    int t;    int i,j,k;    scanf("%d",&t);    while(t--)    {        scanf("%s",str);        n=strlen(str);        printf("%d\n",get_min(str,n)+1);    }    return 0;}

聯繫我們

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