Period (poj 1961&&hdu 1358)KMP,periodpoj1961

來源:互聯網
上載者:User

Period (poj 1961&&hdu 1358)KMP,periodpoj1961

Language:DefaultPeriod
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 13504   Accepted: 6365

Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK ,that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S.The second line contains the string S. The input file ends with a line, having the 
number zero on it.

Output

For each test case, output "Test case #" and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3aaa12aabaabaabaab0

Sample Output

Test case #12 23 3Test case #22 26 29 312 4

Source

Southeastern Europe 2004

題意:求長度為i(2<=i<=N)的首碼,若首碼是一個周期串,則輸出長度i和它的最大周期;要找出所有滿足條件的。

思路:next[i]數組裡面存的是i位置前 字串的相同首碼和尾碼的最大長度,若它是一個周期串,那它必滿足i%(i-next[i])==0.

代碼:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 1000010#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int next[MAXN];char str[MAXN];int N;void get_next(){    int i=0,j=-1;    next[0]=-1;    while (i<N)    {        if (j==-1||str[i]==str[j])        {            i++;            j++;            next[i]=j;        }        else            j=next[j];    }}void KMP(){    for (int i=1;i<=N;i++) //枚舉所有長度,看它滿不滿足要求    {        if (!(i%(i-next[i]))&&(i/(i-next[i]))>1)//滿足就輸出            printf("%d %d\n",i,i/(i-next[i]));    }}int main(){    int cas=1;    while (scanf("%d",&N)&&N)    {        scanf("%s",str);        get_next();        printf("Test case #%d\n",cas++);        KMP();        printf("\n");    }    return 0;}/*3aaa12aabaabaabaab0*/



poj1970 牛人正解(附My Code)

我幫你改了一下,AC了。你沒有記錄成功時棋子的顏色。所以每次搜尋你的位置是對的,但是顏色卻總是最後一個位置的顏色。
#include<stdio.h>
int main()
{
int k,n,a[29][29]={0},i,j,t,s,p,q,tmp;
scanf("%d",&n);
for(k=0;k<n;k++)
{
s=0;
t=0;
for(i=5;i<24;i++)
{
for(j=5;j<24;j++)
scanf("%d",&a[i][j]);
}
for(j=5;j<24;j++)
{
for(i=5;i<24;i++)
{
if(a[i][j]!=0)
{
t=a[i][j];
if(a[i][j-1]!=t&&a[i][j+1]==t&&a[i][j+2]==t&&a[i][j+3]==t&&a[i][j+4]==t&&a[i][j+5]!=t)
{s++;p=i;q=j;tmp=t;}
if(a[i-1][j]!=t&&a[i+1][j]==t&&a[i+2][j]==t&&a[i+3][j]==t&&a[i+4][j]==t&&a[i+5][j]!=t)
{s++;p=i;q=j;tmp=t;}
if(a[i-1][j-1]!=t&&a[i+1][j+1]==t&&a[i+2][j+2]==t&&a[i+3][j+3]==t&&a[i+4][j+4]==t&&a[i+5][j+5]!=t)
{s++;p=i;q=j;tmp=t;}
if(a[i+1][j-1]!=t&&a[i-1][j+1]==t&&a[i-2][j+2]==t&&a[i-3][j+3]==t&&a[i-4][j+4]==t&&a[i-5][j+5]!=t)
{s++;p=i;q=j;tmp=t;}
}
}
}
if(s==0)
printf("0\n");
else
printf("%d\n%d %d\n",tmp,p-4,q-4);
}
return 0;
}
 
北大線上測試POJ取石子問題(1067)解

#include<stdio.h>
#include<math.h>
double p=(sqrt((double)5)+1)/double(2);
int main (){
int a,b,c;
while(scanf("%d%d",&a,&b)!=EOF){
c=abs(a-b);
a=a>b?b:a;
if(a==(int)(p*c)) printf("0\n");
else printf("1\n");
}
return 0;
}

ACM一般都是檔案流結束的,如果題目不特別說明的話
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.