您的位置:首页 > 其它

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

2013-06-17 14:30 417 查看
Problem - 3374

KMP求循环节。
/article/5031263.html

  循环节推导的证明相当的好,这题是很裸的套算法的题。
代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1111111;
char buf
;
int next
;

void getNext(char *str) {
char *si = str;
int *ni = next;
int j = *next = -1;
while (*si) {
while (j > -1 && *si != *(str + j)) j = *(next + j);
si++, ni++, j++;
//        if (*si == *(str + j)) *ni = *(next + j);
//        else *ni = j;
*ni = j;
}
}

int minMaxExp(char *s, bool mini) {
int i = 0, j = 1, k = 0, t;
int len = strlen(s);
while (i < len && j < len && k < len) {
//        cout << i << ' ' << j << ' ' << k << endl;
t = s[(i + k) % len] - s[(j + k) % len];
if (!t) k++;
else {
if (mini ^ (t > 0)) j += k + 1;
else i += k + 1;
if (i == j) j++;
k = 0;
}
}
return min(i, j);
}

int main() {
while (cin >> buf) {
getNext(buf);
//        for (int i = 0, sz = strlen(buf); i < sz; i++) cout << next[i] + 1 << ' '; cout << endl;
int cycle = strlen(buf);
//        cout << "got next" << endl;
cycle /= (cycle - next[cycle - 1] - 1);
cout << minMaxExp(buf, true) + 1 << ' ' << cycle << ' ' << minMaxExp(buf, false) + 1 << ' ' << cycle << endl;
}
return 0;
}


View Code

——written by Lyon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: