日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)

發(fā)布時(shí)間:2025/4/16 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Problem - 3374 KMP求循環(huán)節(jié)。 http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循環(huán)節(jié)推導(dǎo)的證明相當(dāng)?shù)暮?#xff0c;這題是很裸的套算法的題。 代碼如下: 1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <algorithm> 5 6 using namespace std; 7 8 const int N = 1111111; 9 char buf[N]; 10 int next[N]; 11 12 void getNext(char *str) { 13 char *si = str; 14 int *ni = next; 15 int j = *next = -1; 16 while (*si) { 17 while (j > -1 && *si != *(str + j)) j = *(next + j); 18 si++, ni++, j++; 19 // if (*si == *(str + j)) *ni = *(next + j); 20 // else *ni = j; 21 *ni = j; 22 } 23 } 24 25 int minMaxExp(char *s, bool mini) { 26 int i = 0, j = 1, k = 0, t; 27 int len = strlen(s); 28 while (i < len && j < len && k < len) { 29 // cout << i << ' ' << j << ' ' << k << endl; 30 t = s[(i + k) % len] - s[(j + k) % len]; 31 if (!t) k++; 32 else { 33 if (mini ^ (t > 0)) j += k + 1; 34 else i += k + 1; 35 if (i == j) j++; 36 k = 0; 37 } 38 } 39 return min(i, j); 40 } 41 42 int main() { 43 while (cin >> buf) { 44 getNext(buf); 45 // for (int i = 0, sz = strlen(buf); i < sz; i++) cout << next[i] + 1 << ' '; cout << endl; 46 int cycle = strlen(buf); 47 // cout << "got next" << endl; 48 cycle /= (cycle - next[cycle - 1] - 1); 49 cout << minMaxExp(buf, true) + 1 << ' ' << cycle << ' ' << minMaxExp(buf, false) + 1 << ' ' << cycle << endl; 50 } 51 return 0; 52 } View Code

?

——written by Lyon

轉(zhuǎn)載于:https://www.cnblogs.com/LyonLys/p/hdu_3374_Lyon.html

總結(jié)

以上是生活随笔為你收集整理的hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。