hdoj 3068 最長迴文 【manacher演算法】,

來源:互聯網
上載者:User

hdoj 3068 最長迴文 【manacher演算法】,

題意。。。

用傳統的方法來做的話,要逾時(就是要進行奇偶判斷)。

manacher演算法,百度一下講解好的有很多。


紀念粘代碼:

#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define M 110010char a[M], b[M<<1];int p[M<<1];int main(){while(~scanf("%s", &a[1])){//int len = strlen(a);int i, len;for(i = 1; a[i] != '\0'; i ++){b[i<<1] = a[i];b[(i<<1)+1] = '#';}len = (i<<1)+2;b[0] = '@'; b[1] = '#'; b[len] = 0;//printf("%d %s.", len, &b[1]);int maxid, id, max;maxid = max = id = 0;for(i = 1; i < len; i ++){if(maxid > i){p[i] = min(p[2*id-i], maxid-i);}else p[i] = 1;while(b[i-p[i]] == b[i+p[i]]) ++p[i];if(maxid < p[i]+i){  //每一次maxid<p[i+i]就將maxid和id更新。maxid = p[i]+i;id = i;}if(max < p[i]) max = p[i];//printf("%d..", p[i]);}printf("%d\n", max-1);}return 0;} 

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=3068




聯繫我們

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