• hdoj 3294 Girls' research


    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 1027    Accepted Submission(s): 389


    Problem 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!
     

    总结:

    Manacher的基础题,不过需要输出最长回文。

    这里需要能推导出这两个式子

    int a = (ans-1)/2-(MaxL-1)/2;

    int b = a+MaxL-1;

    其中a为最大回文串在原字符串的起点,b为终点。ans为最大回文串在新字符串数组中的中点下标,MaxL为最大回文串长度。

    代码:

    #include <iostream>
    #include <cstring>
    #include <cmath>
    
    using namespace std;
    
    const int MAXN = 200005;
    
    int P[MAXN*2];
    char S[MAXN];
    char N[MAXN*2];
    int MaxL,ans;
    
    void Manacher(int lenN)
    {
    	int id = 0;
    	int mx = 0;
    	for(int i=0 ; i<lenN ; i++)
    	{
    		if(i<mx)
    		{
    			P[i] = min(P[2*id-i],mx-i);
    		}
    		else P[i] = 0;
    		while(i-P[i]>=0 && i+P[i]<lenN &&N[i-P[i]] == N[i+P[i]])P[i]++;
    		if(i+P[i]>mx)
    		{
    			id = i;
    			mx = i+P[i];
    		}
    		if(P[i]-1 > MaxL)
    		{
    			MaxL = P[i]-1;
    			ans = i;
    		}
    	}
    }
    
    int main()
    {
    	char ch;
    	while(scanf("%c %s",&ch,S)!=EOF)
    	{
    		getchar();
    		MaxL = 1;
    		ans = 0;
    		int mid = (int)(ch-'a');
    		int len = strlen(S);
    		int lenN = 0;
    		for(int i=0 ; i<len ; i++)
    		{
    			char t = S[i] - mid;
    			if(t<'a')t += 26;
    			N[lenN++] = '#';
    			N[lenN++] = t;
    		}
    		Manacher(lenN);
    		if(MaxL == 1)printf("No solution!
    ");
    		else
    		{
    			int a = (ans-1)/2-(MaxL-1)/2;
    			int b = a+MaxL-1;
    			printf("%d %d
    ",a,b);
    			for(int i=ans-MaxL ; i<=ans+MaxL ; i++){
    				if(i&1)printf("%c",N[i]);
    			}
    			printf("
    ");
    		}
    	}
    
    	return 0;
    }


  • 相关阅读:
    杭电 2546 饭卡 (01背包问题)
    杭电2602 Bone Collector
    01背包问题的详细概述
    杭电1003 最大子串(第二次AC) 当作DP的训练吧
    杭电1087 Super Jumping! Jumping! Jumping!(初见DP)
    Virtualenv虚拟环境的创建、激活、及退出
    修改maven本地仓库路径
    PPT如何去除背景音乐(史上最全方案)
    NVIDIA显卡驱动安装步骤
    Python之NumPy(axis=0 与axis=1)区分
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514147.html
Copyright © 2020-2023  润新知