fzu1901Period II

地址:http://acm.fzu.edu.cn/problem.php?pid=1901

题目:

Problem 1901 Period II

Accept: 442    Submit: 1099
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

For each prefix with length P of a given string S,if

S[i]=S[i+P] for i in [0..SIZE(S)-p-1],

then the prefix is a “period” of S. We want to all the periodic prefixs.

 Input

Input contains multiple cases.

The first line contains an integer T representing the number of cases. Then following T cases.

Each test case contains a string S (1 <= SIZE(S) <= 1000000),represents the title.S consists of lowercase ,uppercase letter.

 Output

For each test case, first output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of periodic prefixs.Then output the lengths of the periodic prefixs in ascending order.

 Sample Input

4
ooo
acmacmacmacmacma
fzufzufzuf
stostootssto

 Sample Output

Case #1: 3
1 2 3
Case #2: 6
3 6 9 12 15 16
Case #3: 4
3 6 9 10
Case #4: 2
9 12

 Source

FOJ有奖月赛-2010年05月

思路:next数组

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <set>
 5 #include <string>
 6 using namespace std;
 7
 8 #define MP make_pair
 9 #define PB push_back
10 typedef long long LL;
11 const double eps=1e-8;
12 const int K=1e6+7;
13 const int mod=1e9+7;
14
15 int nt[K],ans[K];
16 char sa[K];
17 void kmp_next(char *T,int *next)
18 {
19     next[0]=0;
20     for(int i=1,j=0,len=strlen(T);i<len;i++)
21     {
22         while(j&&T[i]!=T[j]) j=next[j-1];
23         if(T[i]==T[j])  j++;
24         next[i]=j;
25     }
26 }
27 int kmp(char *S,char *T,int *next)
28 {
29     int ans=0;
30     int ls=strlen(S),lt=strlen(T);
31     for(int i=0,j=0;i<ls;i++)
32     {
33         while(j&&S[i]!=T[j]) j=next[j-1];
34         if(S[i]==T[j])  j++;
35         if(j==lt)   ans++;
36     }
37     return ans;
38 }
39
40
41 int main(void)
42 {
43     int t,cnt=1;cin>>t;
44     while(t--)
45     {
46         scanf("%s",sa);
47         kmp_next(sa,nt);
48         int len=strlen(sa),k=0;
49         int tk=len;
50         while(nt[tk-1]>0)
51             ans[k++]=len-nt[tk-1],tk=nt[tk-1];
52         ans[k++]=len;
53         printf("Case #%d: %d\n",cnt++,k);
54         for(int i=0;i<k;i++)
55             printf("%d%c",ans[i],i!=k-1?‘ ‘:‘\n‘);
56     }
57     return 0;
58 }
时间: 2024-11-05 04:48:53

fzu1901Period II的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案

Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据AlteraFAE描述:"EPCS器件也是选用某家公司的SPIFlash,只是中间经过Altera公司的严格测试,所以稳定性及耐用性都超过通用的SPIFlash".就本人看来,半导体的稳定性问题绝大部分都是由本身设计缺陷造成的,而成熟的制造工艺不会造成产品的不稳定:并且,现在Altera的器件在读入配置数据发生错误时,可以重新读取SPIFlash里面的数据,所以在工艺的稳定性以及设计的可靠性

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

AC日记——小A和uim之大逃离 II 洛谷七月月赛

小A和uim之大逃离 II 思路: spfa: 代码: #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f struct NodeType { int x,y,k; NodeType(int x_,int y_,int k_):x(x_),y(y_),k(k_){} NodeType(){} }; const int dx[5]={0,-1,0,1,0}; const int dy[5]={0,0,1,0,-

remove-duplicates-from-sorted-list I&amp;II——去除链表中重复项

I.Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. PS:遍历,而后记录pre,并删除后续重复node 1 /** 2 * Definition for singly-linke

Intersection of Two Arrays I &amp; II

题目链接:https://leetcode.com/problems/intersection-of-two-arrays/ 题目大意:要求两个数组的交集(注意集合是不能含有重复的元素的) 方法1) 先对两个数组进行排序,设置两个指针pA和pB,分别指向这两个数组,比较nums1[pA]和nums[pB] a. 如果想等,则为交集中的元素,++pA, ++pB b. 如果nums[pA] < nums[pB],则++pA c. 否则,++pB 注意数组中有重复的元素(实现代码中的小trick)

Wiggle Sort II

Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 注意事项 You may assume all input has valid answer. 样例 Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. Given nums

1245 - Harmonic Number (II)(规律题)

1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int n ) {     long long res = 0;     for( int i =

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵: