Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一直Time Limit Exceeded是怎么回事?

Posted by kaimei at 2012-09-12 19:20:04 on Problem 2752
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <fstream>
#define MAXN 400000
using namespace std;

char name[MAXN+10];
char temp[MAXN+10];
int len[MAXN+10];
int next[MAXN+10];

int get_next(const char *str)
{
    int j = 0, k = -1;
    memset(next, 0 ,sizeof(next));
    next[0] = -1;
    while(j < strlen(str))
    {
        if(k==-1 || str[j]==str[k])
        {
            ++j;
            ++k;
            //if(str[j] != str[k])
                next[j] = k;
            //else
                //next[j] = next[k];
        }
        else
            k = next[k];
    }
    return next[j];
}
int main()
{
    //freopen("E:\\in.txt","r",stdin);
    while(scanf("%s",name) != EOF )
    {
        len[0] = strlen(name);
        int pos = 1; //记录有多少个cur
        int cur;
        cur = get_next(name);
        //len[pos++] = cur;

        while(cur > 0)
        {
            len[pos++] = cur;
            //int i;
            /*for( i=0; i<cur; i++)    //新的
                temp[i] = name[i];
            temp[i] = '\0';  */

            /*cur = get_next(temp);*/   //没必要再计算了
            cur = next[cur];
            //printf("%d\n",cur);
            //len[pos++] = cur;
        }

        for(int j=pos-1; j>=0; j--)
            printf("%d ",len[j]);
        printf("\n");

        }

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator