马拉车算法

详细讲解:

https://www.cnblogs.com/grandyang/p/4475985.html


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!
#include <cstdio>
#include <cstring>
#include <string>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int mx=0,id=0,maxcenter=0,maxlen=0;

void Manacher(string s){

    string t="$#";
    mx=0,id=0,maxcenter=0,maxlen=0;
    for(int i=0;i<s.size();i++){
        t+=s[i];
        t+='#';
    }//加上字符
    vector<int>p(t.size(),0);//创建一个有t.size()个元素 ,均值为0的整型数组
    for(int i=1;i<t.size();i++){
        p[i]=mx>i?min(p[2*id-i],mx-i):1;
        while(t[i-p[i]]==t[i+p[i]]) ++p[i];//半径长度进行扩展
        if(mx<i+p[i]){
            mx=i+p[i];
            id=i;
        }
        if(maxlen<p[i]){
            maxcenter=i;
            maxlen=p[i];
        }
    }
    //return s.substr((maxcenter-maxlen)/2,maxlen-1); //开始位置 字符串长度
}
int main(){
    char ch;
    string s;
    int len;
    while(scanf("%c",&ch)!=EOF){

        cin>>s;

        Manacher(s);

        if(maxlen<=2) printf("No solution!\n");
        else{
            len=ch-'a';

            printf("%d %d\n",(maxcenter-maxlen)/2,(maxcenter-maxlen)/2+maxlen-2);
            for(int i=(maxcenter-maxlen)/2;i<maxlen-1+(maxcenter-maxlen)/2;i++){
                if(s[i]>=ch) {
                    cout<<(char)(s[i]-len);
                }
                else{
                    cout<<(char)('z'+(s[i]-'a'-len)+1);
                }
            }
            printf("\n");
        }
        getchar();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
马拉车算法是一种常用于字符串匹配的算法,其核心思想是利用回文串的对称性来减少比较次数。Java中可以通过以下步骤实现马拉车算法: 1. 预处理字符串,将字符串中的每个字符用一个特殊字符隔开,如:将字符串"abc"变成"#a#b#c#" 2. 维护一个数组P,其中P[i]表示以i为中心的最长回文子串的半径长度。具体实现时,可以使用一个中心点center和右边界right来维护,其中center表示当前已知的最长回文子串的中心点,right表示该回文子串的右边界。根据回文串的对称性,可以利用已知回文串的左侧字符的对称点来推出右侧字符的回文半径。 3. 遍历字符串,根据P数组更新center和right,并记录最长回文子串的起始位置和长度。 以下是Java代码实现示例: ```java public class ManacherAlgorithm { public static String longestPalindrome(String s) { if (s == null || s.length() == 0) { return ""; } StringBuilder sb = new StringBuilder(); sb.append("#"); for (int i = 0; i < s.length(); i++) { sb.append(s.charAt(i)); sb.append("#"); } String str = sb.toString(); int[] P = new int[str.length()]; int center = 0, right = 0; int start = 0, maxLen = 0; for (int i = 0; i < str.length(); i++) { if (i < right) { P[i] = Math.min(right - i, P[2 * center - i]); } while (i - P[i] - 1 >= 0 && i + P[i] + 1 < str.length() && str.charAt(i - P[i] - 1) == str.charAt(i + P[i] + 1)) { P[i]++; } if (i + P[i] > right) { center = i; right = i + P[i]; } if (P[i] > maxLen) { start = (i - P[i]) / 2; maxLen = P[i]; } } return s.substring(start, start + maxLen); } public static void main(String[] args) { String s = "babad"; System.out.println(longestPalindrome(s)); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值