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

这道题让我背下了kmp算法,在这里再默写一遍

Posted by songruirui at 2013-03-25 20:31:10 on Problem 1961
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;
int mnext[1000001];
void getNext(string str)
{
	int i,j;
	i=0;
	j=-1;
	mnext[0]=-1;
	while(i<str.size())
	{
		if(j==-1||str[i]==str[j])
		{
			i++;
			j++;
			mnext[i]=j;
		}
		else
			j=mnext[j];
	}
}
int main()
{
	int len;
	cin>>len;
	int num=0;
	while(len!=0)
	{
		memset(mnext,0,sizeof(mnext));
		string str;
		cin>>str;
		getNext(str);
		cout<<"Test case #"<<++num<<endl;
		for(int i=2;i<=str.size();i++)
		{
			int len=i-mnext[i];
			if(i%len==0&&i/len>1)
				cout<<i<<" "<<i/len<<endl;
		}
	}
	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