标签:回文 代码 https using while == span har int
题源:https://vjudge.net/problem/UVA-11475
很标准的回文串问题(为什么总是在一些奇奇怪怪的地方写错。。)
#include <iostream> #include <stdio.h> #include <cstring> #define LCOAL #define maxn 100005 using namespace std; char s[maxn], ma[maxn << 1]; int mp[maxn << 1]; //注意开双倍 //参考oiwiki上的代码 int Manacher(char s[]) { int mal = 0; ma[mal++] = ‘#‘; //首位为# int len = strlen(s); for (int i = 0; i < len; ++i) ma[mal++] = s[i], ma[mal++] = ‘#‘; mp[0] = 0; for (int i = 1, l = 0, r = -1; i < mal; ++i) { mp[i] = i > r ? 1 : min(mp[l + r - i], r - i); while (i - mp[i] >= 0 && i + mp[i] < mal && ma[i - mp[i]] == ma[i + mp[i]]) mp[i]++; if (i + mp[i] == mal) return i - mp[i]; if (i + mp[i] > r) { l = i - mp[i]; r = i + mp[i]; } } } int main() { int t; while (~scanf("%s", s)) { printf("%s", s); t = Manacher(s); for (int i = t; i > 0; --i) { if (ma[i] != ‘#‘) putchar(ma[i]); } putchar(‘\n‘); } }
标签:回文 代码 https using while == span har int
原文地址:https://www.cnblogs.com/jionkitten/p/12231263.html