码迷,mamicode.com
首页 > 其他好文 > 详细

ICPC2017 Hua-Lian Finding the Bases(字符串,kmp)

时间:2018-08-27 21:38:40      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:分享   integer   ase   basis   技术分享   ordinary   tween   txt   ignore   

题目描述

There are many ways to represent a string. The following syntax
(x1 , k1 )(x2 , k2 ) . . . (xl , kl )
defines the string 技术分享图片 where xi is the ith string that has to repeat ki times. We call this representation a brief string because it can represent a very long string by using only relatively small amount of space. For example, (ab, 2)(a, 4) represents ababaaaa. If you are given a brief string, certainly you can quickly recover the string that it represents. 
Conversely, if you are given an ordinary string, you can find many di?erent brief strings that represent it. We are interested in finding the shortest one. We define the length of a brief string (x1 , k1 )(x2 , k2 ) . . . (xl , kl ) to be |x1 | + |x2 | + ... + |xl |. That is, we only consider the total length of strings that has to be repeated and ignore all the numbers (as well as the parentheses and commas). The shortest brief string of an ordinary string is called a basis.
For example, both (a, 1)(ba, 3)(a, 3) and (ab, 3)(a, 4) represent the same string abababaaaa.
However, only the second one is its basis whose length is 3. In this problem, you need to find the length of a basis of an ordinary string.

 

输入

The first line of input contains an integer indicating the number of test cases. For each test case, an ordinary string is given on a single line.

 

输出

Output the length of the basis of the speci?ed ordinary string for each test case.

 

样例输入

3
aaaaaaaaaa
abcabcabca
abcdab

 

样例输出

1
4
6

 

提示

1.The alphabet contains the lowercase English letters.
2.The length of an ordinary string is between 1 and 10000.
3.There are at most 20 test cases.

对于题意,要求最短的表达式,当用最小循环元来表示一个字符串时,其表达式才最短。
对于字符串S自匹配求出next数组,分析可以发现:当i-next[i]能整除i时,S[1~i-next[i]]就是S[1~i]的最小循环元。它的最大循环次数就是i/(i-next[i])。
接下来枚举所有字串的最小循环元,取最优。
 
 
 
#include "bits/stdc++.h"

using namespace std;

const int maxn = 1e4 + 100;


int n;
char s[maxn];
int Next[maxn];
int f[maxn];

void getnext(char str[], int l) {
    for (int i = 2, j = 0; i <= l; i++) {
        while (j > 0 && str[i] != str[j + 1]) j = Next[j];
        if (str[i] == str[j + 1]) j++;
        Next[i] = j;
    }
}


int main() {
    //freopen("input.txt", "r", stdin);
    int N, now, temp;
    scanf("%d", &N);
    while (N--) {
        scanf("%s", s + 1);
        n = strlen(s + 1);
        for (int i = 0; i <= n; i++)
            f[i] = i;
        for (int i = 1; i <= n; i++) {
            getnext(s + i - 1, n - i + 1);
            for (int j = i; j <= n; j++) {
                now = j - i + 1;
                if (now % (now - Next[now]) == 0) {
                    f[j] = min(f[j], f[i - 1] + now - Next[now]);
                }
            }
        }
        printf("%d\n", f[n]);
    }
    return 0;
}

 

ICPC2017 Hua-Lian Finding the Bases(字符串,kmp)

标签:分享   integer   ase   basis   技术分享   ordinary   tween   txt   ignore   

原文地址:https://www.cnblogs.com/albert-biu/p/9544124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!