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


    题目描述

    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])。
    接下来枚举所有字串的最小循环元,取最优。
     
     
     
    #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
    ", f[n]);
        }
        return 0;
    }
  • 相关阅读:
    设置光盘为yum源
    oracle11G的安装历程
    数据备份系统治理方案
    设置 MySql 数据同步
    让MySQL与OpenOffice共舞
    My SQL数据库引擎疾速指导1
    MySQL就事器知名网站中的运用量
    MySQL 5.0 新特征教程 存储历程:第三讲
    解决MySQL数据库作古掉以及谢绝任事的办法
    Sun公司将引进新的API对MySQL休止功能调停
  • 原文地址:https://www.cnblogs.com/albert-biu/p/9544124.html
Copyright © 2020-2023  润新知