程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU3294 Girls' research

HDU3294 Girls' research

編輯:關於C++

Girls' research

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

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!

Author wangjing1111
Source 2010 “HDU-Sailormoon” Programming Contest
題意:給出一個字母,例如b,那麼b就是真正的a,c就是真正的b,再給出一個字符串,讓你求出最大回文串。 分析:manacher算法,改一點點東西就行了。
<span style="font-size:18px;">#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include<map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define MAXN 200010

char ch,str[MAXN*2],s[MAXN];
int p[MAXN*2];
int pre,maxx;

void init()
{
    int i,l;
    CL(str, '\0');///初始化str
    str[0] = '$'; str[1] = '#';
    for(i=0,l=2; s[i]; i++,l+=2)
    {
        str[l] = s[i];
        str[l+1] = '#';
    }
}

void solve()
{
    int id;
    int mx = 0;
    maxx = 0;
    for(int i=1; str[i]; i++)
    {
        if(mx > i) p[i]=p[2*id-i]>(mx-i)?(mx-i):p[2*id-i];
        else p[i] = 1;
        while(str[i-p[i]] == str[i+p[i]]) p[i]++;
        if(p[i]+i > mx)
        {
            mx = p[i] + i;
            id = i;
        }
        if(maxx < p[i])
        {///記錄最長回文點的位置
            maxx = p[i]; pre = i;
           // cout<<str<<endl; printf("maxx="%d" pre="%d\n",maxx,pre);" }="" maxx--;="" int="" main()="" {="" while(scanf("%c%s",&ch,s)="=2)" getchar();="" x="ch" -="" &#39;a&#39;;="" for(int="" i="0;" s[i];="" i++)="" t="s[i]" s[i]="(t" +="" 26)%26="" init();="" solve();="" r="pre/2" maxx="" 2="" 1;="" l="r" if(maxx=""> 1)
        {
            printf("%d %d\n",l,r);
            for(int i=l; i<=r; i++)
                printf("%c",s[i]);
            printf("\n");
        }
        else printf("No solution!\n");
    }
    return 0;
}
</str<<endl;></algorithm></cmath></vector></set></map></queue></stack></cstring></cstdio></iostream></span>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved