标签:des style http color io os ar for div
Description
Input
Output
Sample Input
3 aaa abca abcde
Sample Output
0 2 5 题意:求让你添加几个字母使得字符串能构成由若干个循环节组成的字符串 思路:已经有了推论出知道:i-next[i]是最小循环节,需要增加一些特判,当i-next[i]==1的时候代表的是类似“aaa”的字符串,当next[i]==0的时候就需要我们添加字符串的长度 单位了,否则就是判断本身就能构成若干个循环节和需要再添加构成一个循环节#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int maxn = 100010; char pattern[maxn]; int next[maxn]; void getNext() { int m = strlen(pattern); next[0] = 0, next[1] = 0; for (int i = 1; i < m; i++) { int j = next[i]; while (j && pattern[i] != pattern[j]) j = next[j]; next[i+1] = pattern[i] == pattern[j] ? j+1 : 0; } int len = m - next[m]; if (len == 1) printf("0\n"); else if (len == m) printf("%d\n", m); else if (next[m] > 0 && (m % len) == 0) printf("0\n"); else { int num = m / len; printf("%d\n", len-(m-len*num)); } } int main() { int t; scanf("%d", &t); while (t--) { scanf("%s", pattern); getNext(); } return 0; }
HDU - 3746 Cyclic Nacklace (KMP求循环节)
标签:des style http color io os ar for div
原文地址:http://blog.csdn.net/u011345136/article/details/39231397