zoukankan      html  css  js  c++  java
  • 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;
    }


  • 相关阅读:
    如何过滤php中危险的HTML代码
    一个老总的语录
    php如何优化压缩的图片
    php时间函数time(),date(),mktime()区别
    php中获取网站访客来源的关键词方法
    php获取QQ头像并显示的方法
    php一些常规动态设置与获取
    xss过滤函数
    php获取当月的第一天以及最后一天
    php eval函数一句话木马代码
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514147.html
Copyright © 2011-2022 走看看