首页 > 其他分享 >OKR-Periods of Words

OKR-Periods of Words

时间:2024-05-05 16:44:21浏览次数:19  
标签:abab OKR 前缀 period maximum Periods Words proper string

[POI2006] OKR-Periods of Words

题面翻译

对于一个仅含小写字母的字符串 \(a\),\(p\) 为 \(a\) 的前缀且 \(p\ne a\),那么我们称 \(p\) 为 \(a\) 的 proper 前缀。

规定字符串 \(Q\) 表示 \(a\) 的周期,当且仅当 \(Q\) 是 \(a\) 的 proper 前缀且 \(a\) 是 \(Q+Q\) 的前缀。若这样的字符串不存在,则 \(a\) 的周期为空串。

例如 ababab 的一个周期,因为 ababab 的 proper 前缀,且 ababab+ab 的前缀。

求给定字符串所有前缀的最大周期长度之和。

题目描述

A string is a finite sequence of lower-case (non-capital) letters of the English alphabet. Particularly, it may be an empty sequence, i.e. a sequence of 0 letters. By A=BC we denotes that A is a string obtained by concatenation (joining by writing one immediately after another, i.e. without any space, etc.) of the strings B and C (in this order). A string P is a prefix of the string !, if there is a string B, that A=PB. In other words, prefixes of A are the initial fragments of A. In addition, if P!=A and P is not an empty string, we say, that P is a proper prefix of A.

A string Q is a period of Q, if Q is a proper prefix of A and A is a prefix (not necessarily a proper one) of the string QQ. For example, the strings abab and ababab are both periods of the string abababa. The maximum period of a string A is the longest of its periods or the empty string, if A doesn't have any period. For example, the maximum period of ababab is abab. The maximum period of abc is the empty string.

Task Write a programme that:

reads from the standard input the string's length and the string itself,calculates the sum of lengths of maximum periods of all its prefixes,writes the result to the standard output.

输入格式

In the first line of the standard input there is one integer \(k\) (\(1\le k\le 1\ 000\ 000\)) - the length of the string. In the following line a sequence of exactly \(k\) lower-case letters of the English alphabet is written - the string.

输出格式

In the first and only line of the standard output your programme should write an integer - the sum of lengths of maximum periods of all prefixes of the string given in the input.

样例 #1

样例输入 #1

8
babababa

样例输出 #1

24

image
所以我们要用kmp求出前缀函数,然后当前长度减去前缀函数就是周期,求最长周期就是找最小的border
证明:a是Q+Q的前缀并且a的长度<=Q+Q的长度
设当前最小前缀函数长度为len,所以周期T=i-len,len<a
如果a>T+T,那么说明前缀函数发生重合,则中间重合部分字符与开头一定相等,则不为最小前缀函数长度,矛盾,所以a是Q+Q的前缀并且a的长度<=Q+Q的长度

image

点击查看代码
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;
const int N = 1e7;
int k,pi[N];
ll ans=0;char s[N];
void kmp()
{
	int j=0;
	for(int i=2;i<=k;i++)
	{
		while(j&&s[i]!=s[j+1])j=pi[j];
		if(s[i]==s[j+1])j++;
		pi[i]=j;
	}
}

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>k;
	cin>>s+1;
	kmp();
	for(int i=2;i<=k;i++)
	{
		int j=i;
		while(pi[j])j=pi[j];
		if(pi[i])pi[i]=j;
		ans+=i-j;
	}
	cout<<ans;
	return 0;
}

标签:abab,OKR,前缀,period,maximum,Periods,Words,proper,string
From: https://www.cnblogs.com/wlesq/p/18168666

相关文章

  • OKR-Periods of Words
    [POI2006]OKR-PeriodsofWords题面翻译对于一个仅含小写字母的字符串\(a\),\(p\)为\(a\)的前缀且\(p\nea\),那么我们称\(p\)为\(a\)的proper前缀。规定字符串\(Q\)表示\(a\)的周期,当且仅当\(Q\)是\(a\)的proper前缀且\(a\)是\(Q+Q\)的前缀。若这样的......
  • swordswoman
    Ispentalmostmywholelifeinthecastlesilentlyasasimplewoman.Inthedailylife,Ipretendedtobeabeetandweakoldwoman.However,Ihopedtobecomeaswordswomaninthebottomofmyheart.Inthepast,noonebelievedthatawomancouldbeco......
  • OKR实施后公司的显著变化与提升
    自从我司全面推行OKR(Objectives and Key Results,目标与关键成果)管理模式以来,企业的运营效率、团队协作以及战略执行力均实现了显著的优化和提升。首先,在战略执行层面,OKR为我们构建了一条从公司顶层战略到基层执行的清晰路径。通过设定具有挑战性且明确的目标,并配套量化、可衡......
  • CF1200E Compress Words 题解
    题目链接:CF或者洛谷注意到总字符串长度不超过\(1e6\),对于两个串之间找前后缀匹配,只要能暴力枚举长度,\(check\为\O(1)\),那么最后显然线性复杂度。可以考虑\(kmp\),也可以考虑字符串哈希,最好上双哈希,然后拼串显然是在尾部继续添加新的前缀哈希,这个需要添加的串可以单独由匹配......
  • 单词 Play on Words
    原题链接题解我们将一个单词的首字母和尾字母看成两个结点,每个单词代表一条有向边。此时题意为:给你一个有向图,让你找到一条路径,使得仅仅只经过每条边一次。那么题意就是让我们求一个有向图的欧拉回路。code #include<bits/stdc++.h>usingnamespacestd;intfather[30]......
  • P3435 [POI2006] OKR-Periods of Words
    原题链接题解1.Q是S的前缀2.Q!=S3.S是QQ的前缀,且S可以等于QQ4.从S中挖掉Q后剩下的部分与Q(s)的前半部分重合,也就是公共前后缀5.要让Q尽可能长,就要让公共前后缀尽可能短(非零)细节请看代码解答一些疑惑:为什么不能直接求最短公共前后缀,而是要先求最大公共前后缀?code#include<b......
  • 如何召开一次创意十足的OKR头脑风暴会?
    召开一次创意十足的OKR(ObjectivesandKeyResults,目标与关键成果)头脑风暴会,是激发团队成员智慧、明确共同目标并落实关键行动的重要环节。下面将详细列举召开此类头脑风暴会的具体步骤,以确保会议达到预期效果。一、前期准备确定会议目标与主题:明确本次OKR头脑风暴会的核心目标......
  • Elasticsearch 8.x以上实现初始化用户密码,elasticsearch-setup-passwords interactive
    Elasticsearch8.x以上,默认自动开启x-pack验证,在首次启动时,会设置密码,当再次执行elasticsearch-setup-passwordsinteractive就会报错,提示使用elasticsearch-reset-passwords,但是用户太多,还是想要能像8.x以下一直敲回车,设置密码。今天偶然Elasticsearch报错,发现一个方法可以使用,......
  • OKR与KPI(关键绩效指标)有何不同?如何结合使用?
    在现代企业管理中,绩效考核是确保组织目标得以实现的重要手段。其中,OKR(ObjectivesandKeyResults,目标与关键成果)和KPI(KeyPerformanceIndicators,关键绩效指标)是两种常用的绩效管理工具。虽然它们都是为了提升组织绩效而设计的,但在理念、操作方式和应用场景上却存在着明显的不同......
  • OKR如何与组织的整体战略和计划相结合?
    OKR(ObjectivesandKeyResults,目标与关键成果)作为一种流行的目标管理方法,正逐渐成为组织实现战略目标的重要手段。本文将探讨OKR如何与组织的整体战略和计划相结合,从而推动组织的持续发展。首先,我们需要明确OKR与整体战略和计划之间的关系。整体战略是组织长期发展的指导方针,它......