luogu1210 回文检测
Manacher
正确读法:抹内A撤(马拉车)
(跟着假硕学英语)
我们把原来的字符串,通过玄学处理,变成只留下字母,且每两个字母之间有一个奇怪的字符的那种Manacher专用字符串。
建立双射关系f[i]表示a[i]在b中的位置(如果a[i]是字母),f_[i]表示b[i]在a中的位置(如果b[i]是字母)
然后对b数组跑Manacher
然后找最长回文子串回文中心的位置,通过这个位置直接在b数组中先找左右端点,然后在找到a数组中对应位置输出
数组要开到四万(下面的代码开了两万RE一个点,想抄题解的自己改一下):
#include <bits/stdc++.h>
using namespace std;
char a[20010], b[20010];
int f[20010], f_[20010], res[20010];
int main()
{
int p = 0;
while((a[p] = getchar())!=EOF)p++;
a[p]=0;
int len = strlen(a), j = 1;
memset(b, 0x25, sizeof(b));
for (int i = 0; i < len; i++)
{
if(isalpha(a[i]))
{
f[i] = j;
f_[j] = i;
b[j] = a[i];
if(islower(b[j]))
b[j] -= 32;
j+=2;
}
}
//从[0,j)跑马拉车
int more = 0, pos = 0;
for (int i = 0; i < j; i++)
{
if (more > i)
res[i] = min(res[2 * pos - i], more - i);
else
res[i] = 1;
while (i + res[i] < j && i - res[i] >= 0 && b[i+res[i]] == b[i-res[i]])
res[i]++;
if (i + res[i] - 1 > more)
{
more = i + res[i] - 1;
pos = i;
}
}
int maxLengthPos = 0;
for (int i = 1; i < j; i++)
if(res[i] > res[maxLengthPos])
maxLengthPos = i;
int r = maxLengthPos + res[maxLengthPos] - 2, l = maxLengthPos - res[maxLengthPos] + 2;
int L = f_[l], R = f_[r];
//printf("%d %d %d %d\n", l, r, L, R);
printf("%d\n", 1 + (r - l) / 2);
for (int i = L; i <= R; i++)
printf("%c", a[i]);
return 0;
}
让我们一起膜拜大佬林瑞堂@olinr
luogu1210 回文检测的更多相关文章
- C#进行回文检测,判断字符串是否是回文的代码
下面代码内容是关于C#进行回文检测,判断字符串是否是回文的代码,应该是对各位朋友有些好处. Console.WriteLine("算法1:请输入一个字符串!");string st ...
- Manacher【p1210】回文检测
题目描述--->P1210 回文检测 分析: 看到回文显然想到了manacher算法(线性求解回文串问题 如果不了解还是去敲一下板子,学习一下比较好.-->manacher 题目要求我们求 ...
- java_回文检测
package bao; import java.util.Scanner; public class Work { public static boolean digui(String str1,i ...
- 回文检测 [USACO Training Section 1.3]
题目描述 据说如果你给无限只母牛和无限台巨型便携式电脑(有非常大的键盘),那么母牛们会制造出世上最棒的回文.你的工作就是去寻找这些牛制造的奇观(最棒的回文). 在寻找回文时不用理睬那些标点符号.空格( ...
- 「Luogu P1210」回文检测 解题报告
题面 这是一道诡异的黄题 居然让你求一串吧啦吧啦的东西中 字母(大小写)最长的回文串的长度,还要输出完整的串 吐血 思路: 保持淡定,我们啥都不会,就会Manacher,那就用Manacher大法! ...
- 利用python的双向队列(Deque)数据结构实现回文检测的算法
#!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...
- 7.Deque的应用案例-回文检查
- 回文检测:设计程序,检测一个字符串是否为回文. - 回文:回文是一个字符串,读取首尾相同的字符,例如,radar toot madam. - 分析:该问题的解决方案将使用 deque 来存储字符串 ...
- 算法——回文(palindrome)
回文(palindrome):指的是从头读到尾与从尾读到头一模一样的字符串. 分别在C.Java与Python实现回文检测: C: #include <stdio.h> #include ...
- 07 Deque的应用案例-回文检查
- 回文检测:设计程序,检测一个字符串是否为回文. - 回文:回文是一个字符串,读取首尾相同的字符,例如,radar toot madam. - 分析:该问题的解决方案将使用 deque 来存储字符串 ...
随机推荐
- Python数据库(二)-Mysql数据库插入数据
通过python连接mysql数据库,并插入数据 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import pymysql ...
- 将CDM中所有以Relatonship_开头的关系全部重命名,避免生成数据库因为重复关系名报错
Option Explicit ValidationMode = True InteractiveMode = im_Batch Dim mdl '当前model '获取当前活 ...
- 第九章 Servlet工作原理解析(待续)
从 Servlet容器说起 创建 Servlet实例 Servlet体系结构 Servlet如何工作 Servlet中的Listener Filter如何工作 Servlet中的url-pattern
- 11-24网页基础--Js框架及学习思路
第一部分 基本语法: 1.数据类型(字符串.小数.整数.布尔.时间日期)var s="3.14" var n=parsefloat(s) s+=5;var s="abc3 ...
- Solr之缓存篇
原文出自:http://my.oschina.net/u/1026644/blog/123957 Solr在Lucene之上开发了很多Cache功能,从目前提供的Cache类型有: (1)filter ...
- go语言的第一个helloworld
1.新建一个hello.go文件 添加如下代码: package main // 代码包声明语句. import "fmt" //系统包用来输出的 func main() { / ...
- vue 生命周期小结
生命周期小结生命周期钩子的一些使用方法: beforecreate : 可以在这加个loading事件,在加载实例时触发 created : 初始化完成时的事件写在这里,如在这结束loading事件, ...
- GUI编程02
1 编写一个导航栏 from tkinter import * root = Tk() root.title("测试") root.geometry("400x400+4 ...
- Entity Framework Tutorial Basics(36):Eager Loading
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...
- Entity Framework Tutorial Basics(8):Types of Entity in Entity Framework
Types of Entity in Entity Framework: We created EDM for existing database in the previous section. A ...