日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[POI2006]OKR-Periods of Words

發布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [POI2006]OKR-Periods of Words 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

---恢復內容開始---

題目描述

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.

一個串是有限個小寫字符的序列,特別的,一個空序列也可以是一個串. 一個串P是串A的前綴, 當且僅當存在串B, 使得 A = PB. 如果 P A 并且 P 不是一個空串,那么我們說 P 是A的一個proper前綴. 定義Q 是A的周期, 當且僅當Q是A的一個proper 前綴并且A是QQ的前綴(不一定要是proper前綴). 比如串 abab 和 ababab 都是串abababa的周期. 串A的最大周期就是它最長的一個周期或者是一個空串(當A沒有周期的時候), 比如說, ababab的最大周期是abab. 串abc的最大周期是空串. 給出一個串,求出它所有前綴的最大周期長度之和.。

輸入輸出格式

輸入格式:

?

In the first line of the standard input there is one integer?kk?(1\le k\le 1\ 000\ 0001k1?000?000?) - the length of the string. In the following line a sequence of exactly?kk?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:?
8 babababa 輸出樣例#1:?
24 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=1000000+5; 5 6 char a[maxn]; 7 int net[maxn]; 8 int n; 9 ll ans; 10 11 inline void fnet() 12 { 13 net[1]=0; 14 for(int i=2;i<=n;i++) 15 { 16 int now=i-1; 17 while(true) 18 { 19 now=net[now]; 20 if(a[now+1]==a[i]) 21 { 22 net[i]=now+1; 23 break; 24 } 25 if(now==0) 26 break; 27 } 28 } 29 return ; 30 } 31 32 33 int main() 34 { 35 scanf("%d",&n); 36 scanf("%s",a+1); 37 fnet(); 38 for(int i=1;i<=n;i++) 39 { 40 int now=i; 41 while(net[now]!=0) 42 { 43 now=net[now]; 44 } 45 if(net[i]!=0) 46 { 47 net[i]=now; 48 } 49 ans+=i-now; 50 } 51 printf("%lld\n",ans); 52 return 0; 53 }

?

---恢復內容結束---

轉載于:https://www.cnblogs.com/Hammer-cwz-77/p/8546242.html

總結

以上是生活随笔為你收集整理的[POI2006]OKR-Periods of Words的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。