k-substrings Codeforces - 961F
不会做。。
官方题解:
1.枚举某个前缀(指题目要求的相同前后缀中的前缀)的中心位置i,那么对应后缀的中心位置已经确定了(n-i+1),可以二分答案求出对于每个中心位置i最大的符合要求的相同前后缀(设长度为2x+1),然后更新ans[i-x]为2x+1;最后把每个ans[i]用ans[i-1]-2尝试更新一下
2.其实以上做法也基于这个结论?
ans[i-1]<=ans[i]+2,这个可以容易地用反证法证明(类似kmp)
因此从ans[(n+1)/2]开始求就行了
#include<cstdio>
#include<algorithm>
using namespace std;
namespace Hash
{
typedef unsigned long long ULL;
const int md2=;
struct Hdata
{
ULL d1;int d2;
Hdata(ULL a,int b):d1(a),d2(b<md2?(b<?b+md2:b):b-md2){}
Hdata():d1(),d2(){}
};
Hdata operator*(const Hdata &a,int b) {return Hdata(a.d1*b,ULL(a.d2)*b%md2);}
Hdata operator*(const Hdata &a,const Hdata &b) {return Hdata(a.d1*b.d1,ULL(a.d2)*b.d2%md2);}
Hdata operator+(const Hdata &a,int b) {return Hdata(a.d1+b,a.d2+b);}
Hdata operator-(const Hdata &a,const Hdata &b) {return Hdata(a.d1-b.d1,a.d2-b.d2);}
bool operator==(const Hdata &a,const Hdata &b) {return a.d1==b.d1&&a.d2==b.d2;}
}
using Hash::Hdata;
char s[];
Hdata hs[];
const int X=;
Hdata pwx[];
Hdata ghash(int l,int r)
{
if(l>r) return Hdata(,);
return hs[r]-hs[l-]*pwx[r-l+];
}
int n,ans[],l;
bool issame(int l1,int r1,int l2,int r2)
{
if(l1<||l1>n||r1<||r1>n||l2<||l2>n||r2<||r2>n) return ;
return ghash(l1,r1)==ghash(l2,r2);
}
int main()
{
int i,j;
scanf("%d%s",&n,s+);
for(i=;i<=n;i++) hs[i]=hs[i-]*X+s[i];
pwx[]=Hdata(,);
for(i=;i<=n;i++) pwx[i]=pwx[i-]*X;
l=(n+)/;
if(n%==) ans[l]=-;
else ans[l]=s[l]==s[l+]?:-;
for(i=l-;i>=;i--)
{
for(j=ans[i+]+;j!=-&&!issame(i,i+j-,n-i-j+,n-i+);j-=);
ans[i]=j;
}
for(i=;i<=l;i++) printf("%d ",ans[i]);
return ;
}
k-substrings Codeforces - 961F的更多相关文章
- Good Substrings CodeForces - 271D
You've got string s, consisting of small English letters. Some of the English letters are good, the ...
- Erasing Substrings CodeForces - 938F (字符串dp)
大意: 给定字符串$s$, 长度为$n$, 取$k=\lfloor log2(n)\rfloor$, 第$i$次操作删除一个长度为$2^{i-1}$的子串, 求一种方案使得, $k$次操作后$s$的字 ...
- Codeforces 961F - k-substrings(二分+哈希)
Codeforces 题面传送门 & 洛谷题面传送门 介绍一种奇怪的 \(\Theta(n\log n)\) 的奇怪做法. 注意到这个"border 的长度必须是奇数"的条 ...
- Many Equal Substrings CodeForces - 1029A (kmp next数组应用)
题目大意 题目看样例也能猜到就是输出最短的循环串. 吐槽 明明是div3第一题为啥子还会用到kmp的知识? 解法 这个题仔细看发现是求最长可去除的后缀,也就是说去除跟下一个相同的字符串还能连接起来.这 ...
- Codeforces Global Round 8 B. Codeforces Subsequences(构造)
题目链接:https://codeforces.com/contest/1368/problem/B 题意 构造最短的至少含有 $k$ 个 $codeforces$ 子序列的字符串. 题解 如下表: ...
- codeforces271D
Good Substrings CodeForces - 271D 给你一个只包含小写字母的字符串s.问你在这个字符串中有多少个不同的子串.且要求这些子串中不得出现超过k个的特殊字母.*子串s1和子串 ...
- 【HDU 5030】Rabbit's String (二分+后缀数组)
Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...
- hdu 5030 Rabbit's String(后缀数组&二分法)
Rabbit's String Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
随机推荐
- 杭电1232畅通project
畅通project Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- ubuntu 安装后要做的事情
1. 安装chrome,软件中心就可以. 2. 安装vim 和一些插件.这里引入一大牛配置的插件集 sudo apt-get install vim-gtk wget -qO- https://raw ...
- centos7 安装配置openstack-dashboard (官网openstack-juno版)
感谢朋友支持本博客.欢迎共同探讨交流.因为能力和时间有限.错误之处在所难免.欢迎指正! 假设转载.请保留作者信息. 博客地址:http://blog.csdn.net/qq_21398167 原博文地 ...
- Struts 1 Struts 2
Key Technologies Primer https://struts.apache.org/primer.html Threads With Struts 1 you were require ...
- TPC-H is a Decision Support Benchmark
TPC-H is a Decision Support Benchmark http://www.dba-oracle.com/t_tpc_benchmarks.htm
- Statement 与 PreparedStatement 区别
Statement由方法createStatement()创建,该对象用于发送简单的SQL语句 PreparedStatement由方法prepareStatement()创建,该对象用于发送带有一个 ...
- Latex 1: 解决latex中遇到一个常见错误:"Improper alphabetic constant."
1.问题: 本人是在WIN7下用texlive 2016,编辑器用的是WinEdt 10.1 ,运行如下代码: \documentclass{ctexbook} \begin{document} \t ...
- linux 下RTL8723/RTL8188调试记录(命令行)【转】
本文转载自:http://blog.h5min.cn/wuhongxin123/article/details/41820877 本文是在正确安装好wifi驱动后对系统进行的配置. 1. 配置wp ...
- POJ3164 Command Network —— 最小树形图
题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS Memory Limit: 131072K ...
- nginx 静态网站配置
/************************************************************************************** * nginx 静态网站 ...