zoukankan      html  css  js  c++  java
  • LightOJ 1258 (Manacher 或者KMP都行)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=96545#problem/D

    Description:

    A string is said to be a palindrome if it remains same when read backwards. So, 'abba', 'madam' both are palindromes, but 'adam' is not.

    Now you are given a non-empty string S, containing only lowercase English letters. The given string may or may not be palindrome. Your task is to make it a palindrome. But you are only allowed to add characters at the right side of the string. And of course you can add any character you want, but the resulting string has to be a palindrome, and the length of the palindrome should be as small as possible.

    For example, the string is 'bababa'. You can make many palindromes including

    bababababab

    babababab

    bababab

    Since we want a palindrome with minimum length, the solution is 'bababab' cause its length is minimum.

    Input:

    Input starts with an integer T (≤ 10), denoting the number of test cases.

    Each case starts with a line containing a string S. You can assume that 1 ≤ length(S) ≤ 106.

    Output:

    For each case, print the case number and the length of the shortest palindrome you can make with S.

    Sample Input:

    4

    bababababa

    pqrs

    madamimadam

    anncbaaababaaa

    Sample Output:

    Case 1: 11

    Case 2: 7

    Case 3: 11

    Case 4: 19

    题意:有一个字符串,不一定是回文串,现在问最少要添多少字符才能使该字符串变为回文串,输出最终回文串的最小长度。看完就觉得是Manacher,但是怎么改都wa了。。。后来发现其实只要找到以字符串结尾结束的最长回文串的长度就好了。。。。但是有不少人用的Kmp(个人觉得Manacher更好理解)。

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<stdlib.h>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    const int N=1e6+10;
    const int MOD=1e9+7;
    const int INF=0x3f3f3f3f;
    
    char a[N], s[2*N];
    int k, r[2*N];
    
    int Palindrome()
    {
        int Max = 0, id = 0, i;
    
        memset(r, 0, sizeof(r));
    
        for (i = 2; i < k; i++)
        {
            r[i] = 1;
    
            if (r[id]+id > i) r[i] = min(r[2*id-i], r[id]+id-i);
    
            while (s[i-r[i]] == s[i+r[i]]) r[i]++;
    
            if (r[i]+i > r[id]+id) id = i;
    
            if (r[i]-1 > Max && r[i]+i == k)
                Max = r[i]-1;
        }
    
        return Max;
    }
    
    int main ()
    {
        int T, i, len, num = 0, Max;
    
        scanf("%d", &T);
    
        while (T--)
        {
            scanf("%s", a);
            len = strlen(a);
    
            k = 2;
            num++;
    
            memset(s, 0, sizeof(s));
    
            s[0] = '$'; s[1] = '#';
    
            for (i = 0; a[i] != ''; i++)
            {
                s[k++] = a[i];
                s[k++] = '#';
            }
    
            Max = Palindrome();
    
            printf("Case %d: %d
    ", num, len+(len-Max));
        }
    
        return 0;
    }
  • 相关阅读:
    ExtJs2.0学习系列(1)Ext.MessageBox
    PDF加水印
    ExtJs2.0学习系列(2)Ext.Panel
    负载均衡(续)
    位运算设置权限续(转)
    Excel导出问题解决方案(导出时前面的0自动被去掉)
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
    位运算设置权限
    WCF开发实战系列一:创建第一个WCF服务
    通过SQL Server的位运算功能巧妙解决多选查询
  • 原文地址:https://www.cnblogs.com/syhandll/p/4907442.html
Copyright © 2011-2022 走看看