UVA12467 Secret Word 题解
前置知识
解法
考虑将 \(S\) 翻转后得到 \(S'\),然后就转化为求 \(S'\) 的一个最长子串使得其是 \(S\) 的前缀。使用 KMP 求解即可。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define sort stable_sort
#define endl '\n'
int nxt[1000002];
char s1[1000002],s2[1000002];
int main()
{
int t,n,ans,i,j,k;
cin>>t;
for(k=1;k<=t;k++)
{
cin>>(s2+1);
n=strlen(s2+1);
ans=0;
for(i=1;i<=n;i++)
{
s1[i]=s2[n-i+1];
}
for(i=2,nxt[1]=j=0;i<=n;i++)
{
while(j>=1&&s2[i]!=s2[j+1])
{
j=nxt[j];
}
j+=(s2[i]==s2[j+1]);
nxt[i]=j;
}
for(i=1,j=0;i<=n;i++)
{
while(j>=1&&(j==n||s1[i]!=s2[j+1]))
{
j=nxt[j];
}
j+=(s1[i]==s2[j+1]);
ans=max(ans,j);
if(j==n)
{
j=nxt[n];
}
}
for(i=ans;i>=1;i--)
{
cout<<s2[i];
}
cout<<endl;
}
return 0;
}
UVA12467 Secret Word 题解的更多相关文章
- 2021.08.30 前缀函数和KMP
2021.08.30 前缀函数和KMP KMP算法详解-彻底清楚了(转载+部分原创) - sofu6 - 博客园 (cnblogs.com) KMP算法next数组的一种理解思路 - 挠到头秃 - 博 ...
- [Swift]LeetCode843. 猜猜这个单词 | Guess the Word
This problem is an interactive problem new to the LeetCode platform. We are given a word list of uni ...
- [LeetCode] 843. Guess the Word 猜单词
This problem is an interactive problem new to the LeetCode platform. We are given a word list of uni ...
- Moneybookers API支付方式开发 步骤
开发文档: 支付说明手册 步骤: 1.使用商家帐号,登录到www.moneybookers.com,核对商家信息是否正确. 2.在账户-->商家工具(设置) a.API/MQI password ...
- MetaWeblog API调用
http://rpc.cnblogs.com/metaweblog/webenh 在网上闲逛,突然对博客的接口感兴趣,经考察,多数博客都对metaWeblog Api 提供了支持,虽然windows ...
- TPLink 备份文件bin文件解析[续]
Most routers allow to save and restore configuration from files. This is cool because you can edit t ...
- asp.net批量发布博客到各大博客平台
新浪博客 http://upload.move.blog.sina.com.cn/blog_rebuild/blog/xmlrpc.php 网易博客 http://os.blog.163.com/ap ...
- GUI & Event例子
Student No.: _______________ Name: ________________________________________1TK2934 Object-Oriented P ...
- C++ Primer Plus (Stephen Prata 著)
第1章 预备知识 (已看) 第2章 开始学习C++ (已看) 第3章 处理数据 (已看) 第4章 复合类型 (已看) 第5章 循环和关系表达式 (已看) 第6章 分支语句和逻辑运算符 (已看) 第7章 ...
- C语言程序设计
从 大一下 开始写博客,记录自己的学习. 而作为一个强迫症(比如我)是不允许做事缺头少尾的. 想了想还是得大一所学的C语言也搬上来,虽然结构老师经常嘲讽 (也不算嘲讽,算是絮叨吧)我的C语言,但是我还 ...
随机推荐
- plsql打开报错:Control 'dxDockBrowserPanel' has no parent window问题解决
一.现象: 使用plsql登陆oracle数据库时,登陆信息没有报错,但是最后一步报错,重启电脑依然没有解决 一直报:" Control 'dxDockBrowserPanel' has n ...
- JavaScriptif while for switch流程控制 JS函数 内置对象
1,if else语句 2,if else if else语句 3,switch语句 4,for循环 5,while循环 6,三元运算 7,JS中的函数 8,JS中的匿名函数 9,JS中的立即执行函数 ...
- spring启动流程 (5) Autowired原理
构造方法参数Autowire BeanClass可以在构造方法上标注@Autowired注解,Spring在创建Bean实例时将自动为其注入依赖参数 Spring会优先使用标注@Autowired注解 ...
- 解决python报错:ModuleNotFoundError: No module named '_sysconfigdata_x86_64_conda_linux_gnu'
技术背景 在上一篇博客中执行过conda的更新以及用conda安装了gxx_linux-64之后,再执行pip的一些指令时,就会给出如下所示的报错: $ python3 -m pip list Tra ...
- 用C# WPF简单实现仪表控件
时间如流水,只能流去不流回! 点赞再看,养成习惯,这是您给我创作的动力! 本文 Dotnet9 https://dotnet9.com 已收录,站长乐于分享dotnet相关技术,比如Winform.W ...
- MyBatis_问题解决:Invalid bound statement (not found)
Invalid bound statement (not found)问题,即在mybatis中dao接口与mapper配置文件在做映射绑定的时候出现问题,简单说,就是接口与xml要么是找不到,要么是 ...
- Nginx 配置文件备忘单
Nginx 是用于 Web 服务.反向代理.缓存.负载平衡.媒体流等的开源软件.在这篇文章中,我将提到一些我们经常使用的 Nginx 配置. 监听端口 server { Standard HTTP P ...
- [转帖]sql server 索引阐述系列六 碎片查看与解决方案
https://www.cnblogs.com/MrHSR/p/9365720.html 一 . dm_db_index_physical_stats 重要字段说明 1.1 内部碎片:是avg_pag ...
- [转帖]tidb Modify Configuration Dynamically
https://docs.pingcap.com/tidb/v6.5/dynamic-config This document describes how to dynamically modify ...
- [转帖]CentOS-7-x86_64-Everything-2009 rpm包列表(CentOS7.9)
CentOS-7-x86_64-Everything-2009 rpm包列表(CentOS7.9) 共10073个文件 复制389-ds-base-1.3.10.2-6.el7.x86_64.rpm ...