zoukankan      html  css  js  c++  java
  • (回文串 Manacher )Girls' research -- hdu -- 3294

    http://acm.hdu.edu.cn/showproblem.php?pid=3294

    Girls' research
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

    Description

    One day, sailormoon girls are so delighted that they intend to research about palindromic strings. Operation contains two steps: 
    First step: girls will write a long string (only contains lower case) on the paper. For example, "abcde", but 'a' inside is not the real 'a', that means if we define the 'b' is the real 'a', then we can infer that 'c' is the real 'b', 'd' is the real 'c' ……, 'a' is the real 'z'. According to this, string "abcde" changes to "bcdef". 
    Second step: girls will find out the longest palindromic string in the given string, the length of palindromic string must be equal or more than 2.
     

    Input

    Input contains multiple cases. 
    Each case contains two parts, a character and a string, they are separated by one space, the character representing the real 'a' is and the length of the string will not exceed 200000.All input must be lowercase. 
    If the length of string is len, it is marked from 0 to len-1.
     

    Output

    Please execute the operation following the two steps. 
    If you find one, output the start position and end position of palindromic string in a line, next line output the real palindromic string, or output "No solution!". 
    If there are several answers available, please choose the string which first appears.
     

    Sample Input

    b babd
    a abcd
     

    Sample Output

    0 2
    aza
    No solution!
     
    只要找到起始点,一切就OK了!!!
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    
    using namespace std;
    #define INF 0x3f3f3f3f
    #define N 1000007
    
    char s[N], S[N], str[N];
    int  p[N], Mid;
    
    int Manacher()
    {
        int MaxLen = 0, index = 0, ans = 1;
        int len = strlen(s);
        Mid = 0;
    
        for(int i=2; i<len; i++)
        {
            if(MaxLen>i) p[i] = min(p[index*2-i], MaxLen-i);
            else p[i] = 1;
    
            while(s[i-p[i]]==s[i+p[i]] )
                p[i]++;
    
            if(p[i]+i>MaxLen)
            {
                MaxLen = p[i] + i;
                index = i;
            }
    
            if(p[i]>ans)
            {
                ans = p[i];
                Mid = i;
            }
        }
        return ans-1;
    }
    
    int main()
    {
        char ch[10];
    
        while(scanf("%s%s", ch, S)!=EOF)
        {
            int i, len = strlen(S), number;
            char c;
    
            number = ch[0]-'a';
    
            s[0] = '$';
            for(i=0; i<len; i++)
            {
                c = S[i] - number;
                if(c<'a')
                    c += 26;
                S[i] = c;
    
                s[i*2+1] = '#';
                s[i*2+2] = S[i];
            }
            s[i*2+1] = '#';
            s[i*2+2] = 0;
    
            int ans = Manacher();
    
            if(ans<2)
               printf("No solution!
    ");
            else
            {
                int L = Mid/2-(ans+1)/2;
                printf("%d %d
    ", L, L+ans-1);
                memset(str, 0, sizeof(str));
                strncpy(str, S+L, ans);
                printf("%s
    ", str);
            }
        }
        return 0;
    }
    View Code
    勿忘初心
  • 相关阅读:
    【LCT维护基环内向树森林】BZOJ4764 弹飞大爷
    【LCT】BZOJ3091 城市旅行
    【LCT+主席树】BZOJ3514 Codechef MARCH14 GERALD07加强版
    【最大权闭合子图】bzoj4873 [Shoi2017]寿司餐厅
    【LCT】BZOJ2049 [SDOI2008]Cave 洞穴勘测
    【有上下界的网络流】ZOJ2341 Reactor Cooling(有上下界可行流)
    【费用流】BZOJ1061: [Noi2008]志愿者招募(这题超好)
    从输入url到页面加载的过程
    forEach和map的区别
    理解 JavaScript 对象原型、原型链如何工作、如何向 prototype 属性添加新的方法。
  • 原文地址:https://www.cnblogs.com/YY56/p/4853644.html
Copyright © 2011-2022 走看看