L - Finding the Bases UVALive - 8274 (kmp nex数组用法)


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 different 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 specified 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])。
接下来枚举所有字串的最小循环元,取最优。
代码:


//Full of love and hope for the life

//https://paste.ubuntu.com/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define BIG  10000
#define inf 0x3f3f3f3f

typedef long long ll;

using namespace std;

int nex[10010];

void getnext(char s[],int len){//nex数组处理
     for(int i=2,j=0;i<=len;i++){
        while(j>0&&s[i]!=s[j+1]){
            j=nex[j];
        }
        if(s[i]==s[j+1]){
            j++;
        }
        nex[i]=j;
     }
}

int main(){
    int a;
    int f[10010];
    char s[10010];
    scanf("%d",&a);
    while(a--){
        scanf("%s",s+1);
        int len=strlen(s+1);
        for(int i=0;i<=len;i++){
            f[i]=i;
        }
        for(int i=1;i<=len;i++){
            getnext(s+i-1,len-i+1);//一块一块查
            for(int j=i;j<=len;j++){
                int now=j-i+1;
                if(now%(now-nex[now])==0){//是否记录
                    f[j]=min(f[j],f[i-1]+now-nex[now]);//取最小
                }
            }
        }
        printf("%d\n",f[len]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZZ --瑞 hopeACMer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值