hdu 3336 next數組統計首碼次數

來源:互聯網
上載者:User

首碼次數總和=首碼種類數+每種首碼在後面的出現次數

貼一個不錯的題解,但是題解有個地方講的不是太清晰,就是為什麼next【j】+1==next【j+1】的時候就不統計了呢?

http://972169909-qq-com.iteye.com/blog/1114968

因為如果next等於後一個next值加一的話,這次的統計會在後面也被統計到,所以忽略

#include<stdio.h>#include<string.h>#define val 200005int next[val],lenp;char p[val];void set_next();int main(){int cas,i,ans;for(scanf("%d",&cas);cas;cas--){scanf("%d",&lenp);scanf("%s",p);set_next();ans=lenp+next[lenp];for(i=lenp-1;i>=1;i--)if(next[i]!=next[i+1]-1)ans+=next[i];printf("%d\n",ans%10007);}return 0;}void set_next(){int k,j;k=-1,j=0;next[j]=k;while(j<lenp){if(k==-1||p[j]==p[k]){k++;j++;next[j]=k;}else k=next[k];}}

聯繫我們

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