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表
随机推荐
- Linux 命令 sudo
sudo 这个命令. 是为了 让 普通用户 ,也能够以root的身份来运行 操作, 而这些普通用户 又不须要知道root的password. 在 sudo 运行命令的时候. 仅仅须要 输入自己的pas ...
- Zookeeper中的FastLeaderElection选举算法简述
Zookeeper是一个开源的分布式应用协调项目, 当中为了保证各节点的协同工作,Zookeeper在工作时须要有一个Leader. 而Leader是怎样被选举出来的?Zookeep中使用的缺省算法称 ...
- Process类的使用
Process process= new Process(); process.StartInfo.CreateNoWindow = true; process.StartInfo.UseShellE ...
- WHU-1551-Pairs(莫队算法+分块实现)
Description Give you a sequence consisted of n numbers. You are required to answer how many pairs of ...
- scikit-learn:class and function reference(看看你究竟掌握了多少。。)
http://scikit-learn.org/stable/modules/classes.html#module-sklearn.decomposition Reference This is t ...
- GIS+=地理信息+云计算技术——Spark集群部署
第一步:安装软件 Spark 1.5.4:wget http://www.apache.org/dyn/closer.lua/spark/spark-1.5.2/spark-1.5.2 ...
- LIKIE INSTR
SELECT url FROM test_url WHERE FROM_UNIXTIME(create_time,'%Y%m%d %H') < '20171218 00' AND no ...
- res_d_l =[{'contents':d.contents,'href':d.attrs['href']} for d in rd] 泛型
from selenium import webdriverfrom selenium.webdriver.chrome.options import Options# from selenium.w ...
- HDU - 1863 畅通工程(最小生成树)
d.m个村庄,n条路,计算出所有村庄畅通需要的最低成本. s.最小生成树 c.Prim算法:cost[a][b]和cost[b][a]都得赋值. /* Prim算法 Prim求MST 耗费矩阵cost ...
- 解析javascript变量
//add by tim//提供解析javascript 脚本的变量集合 using System;using System.Collections.Generic;using System.Linq ...