ACM集训-马拉车算法

HDU3068 最长回文

解题思路

  求给定字符串最长回文长度,马拉车算法模板题。

AC代码

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
char s[MAXN],str[MAXN << 1];
int p[MAXN << 1];
void manacher() {
    while (~scanf("%s",&s)) {
        int ind = 0;
        str[ind++] = '@';
        str[ind++] = '#';
        for (int i = 0; s[i]; i++) {
            str[ind++] = s[i];
            str[ind++] = '#';
        }
        str[ind] = '\0';
        int mid = 0,mx = 0,c = 0,r = 0;
        p[0] = 0;
        for (int i = 1; i < ind; i++) {
            p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
            while (str[i + p[i]] == str[i - p[i]]) {
                p[i]++;
            }
            if (p[i] + i > mx) {
                mx = p[i] + i;
                mid = i;
            }
            if (p[i] > r) {
                r = p[i];
                c = i;
            }
        }
        printf("%d\n",r - 1);
    }
}

int main() {
    manacher();
    return 0;
}

SPOJ - NUMOFPAL Number of Palindromes

题目大意

  求一个字符串中包含几个回文子串。
  (1)单个字母也是回文。
  (2)如果同一个回文出现不止一次,则应该分别计算。

对于样例 malayalam 有:
m + a + l + a + y + a + l + a + m + malayalam + ala + alayala + layal + aya  + ala 共15

解题思路

  用马拉车求出以每个字母为对称轴的回文串半径,回文串半径就是这个回文串包含的子回文串个数。
  我们插入了’#'字符,相当于我们把真实的回文半径扩大了两倍,p[i] 代表处理后以 i 为中心的回文半径,也代表了以 i 为中心的子回文串的个数,则真实子回文串个数为p[i] / 2。

AC代码

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
char s[MAXN],str[MAXN << 1];
int p[MAXN << 1];
void manacher() {
    while (~scanf("%s",&s)) {
        int ind = 0;
        str[ind++] = '@';
        str[ind++] = '#';
        for (int i = 0; s[i]; i++) {
            str[ind++] = s[i];
            str[ind++] = '#';
        }
        str[ind] = '\0';
        int mid = 0,mx = 0,c = 0,r = 0;
        p[0] = 0;
        int ans = 0;
        for (int i = 1; i < ind; i++) {
            p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
            while (str[i + p[i]] == str[i - p[i]]) {
                p[i]++;
            }
            if (p[i] + i > mx) {
                mx = p[i] + i;
                mid = i;
            }
            if (p[i] > r) {
                r = p[i];
                c = i;
            }
            ans += p[i] / 2;
        }
        printf("%d\n",ans);
    }
}

int main() {
    manacher();
    return 0;
}

HDU3294 Girls’ research

题目大意

  给定一个字符ch和一个字符串s。给定的字符ch对应真正的’a’,其他字母依次对应。求出给定字符串中最长回文串(长度大于1),若存在输出起始位置、终止位置和字符串,否则输出No solution!

解题思路

  将字符串转化后用马拉车求出最长回文串的起始位置和长度即可。

AC代码

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
char s[MAXN],str[MAXN << 1],ch[2];
int p[MAXN << 1];
void manacher() {
    while (~scanf("%s %s",&ch,&s)) {
        int d = ch[0] - 'a';
        int ind = 0;
        str[ind++] = '@';
        str[ind++] = '#';
        for (int i = 0; s[i]; i++) {
        	// 转化
            s[i] -= d;
            if (s[i] < 'a') {
                s[i] += 26;
            }
            str[ind++] = s[i];
            str[ind++] = '#';
        }
        str[ind] = '\0';
        int mid = 0,mx = 0,c = 0,r = 0;
        p[0] = 0;
        int ans = 0;
        for (int i = 1; i < ind; i++) {
            p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
            while (str[i + p[i]] == str[i - p[i]]) {
                p[i]++;
            }
            if (p[i] + i > mx) {
                mx = p[i] + i;
                mid = i;
            }
            if (p[i] > r) {
                r = p[i];
                c = i;
            }
        }
        if (r > 2) {
            int start = (c - r) / 2,end = start + r - 1;
            printf("%d %d\n",start,end - 1);
            for (int i = start; i < end; i++) {
                putchar(s[i]);
            }
            putchar('\n');
        } else {
            puts("No solution!");
        }
    }
}

int main() {
    manacher();
    return 0;
}

CF1326D2 Prefix-Suffix Palindrome (Hard version)

题目大意

  对于给定的只包含小写字母的字符串 s,找到一个最长回文子串 t,使得 t = a + b(“+”表示拼接)。其中a是s的前缀串,b是s的后缀串。

解题思路

  双指针从两头向中间找。先找到两边能构成的最长回文串,再对中间的子字符串temp使用马拉车算法求最长回文子串,要求求得的回文子串必须是temp的前缀串或者是temp的后缀串,这样才能得到合法的s。

AC代码

#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 1e6 + 5;
const int INF = 0x3f3f3f3f;
char str[MAXN << 1],s[MAXN];
int p[MAXN << 1];
void manacher(int left, int right) {
    int ind = 0;
    str[ind++] = '@';
    str[ind++] = '#';
    for (int i = left; i < right; i++) {
        str[ind++] = s[i];
        str[ind++] = '#';
    }
    str[ind] = '\0';
    int mid = 0,mx = 0,c = 0,r = 0;
    p[0] = 0;
    for (int i = 1; i < ind; i++) {
        p[i] = mx > i ? min(p[(mid << 1) - i],mx - i) : 1;
        while (str[i + p[i]] == str[i - p[i]]) {
            p[i]++;
        }
        if (p[i] + i > mx) {
            mx = p[i] + i;
            mid = i;
        }
        if (p[i] > r) {
            int start = (i - p[i]) / 2, end = start + p[i] - 1;
            // 必须是前缀串或后缀串
            if (start == 0 || end == right - left) {
                r = p[i]; 
                c = i;
            }
        }
    }
    // 找到的合法的回文串在原串中的起点和终点
    int start = (c - r) / 2 + left, end = start + r - 1;
    for (int i = start; i < end; i++) {
        putchar(s[i]);
    }
}
void solve() {
    scanf("%s",&s);
    int len = strlen(s) - 1;
    for (int i = 0,j = len; i < j; i++,j--) {
        if (s[i] != s[j]) {
            for (int k = 0; k < i; k++) {
                putchar(s[k]);
            }
            manacher(i,j + 1);
            for (int k = j + 1; k <= len; k++) {
                putchar(s[k]);
            }
            putchar('\n');
            return ;
        }
    }
    printf("%s\n",s);
}

int main() {   
    int T;
    scanf("%d",&T);
    while (T--) {
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值