www.pudn.com > NumStringAdd.rar > main.cpp, change:2009-08-21,size:912b


#include <iostream> 
#include <string> 
#include "hstring.h" 
using namespace std; 
void get_next(string s,int next[]) 
{ 
	int i=1; 
	next[1]=0; 
	int j=0; 
	string::iterator T=s.begin(); 
	while(i<s.length()) 
	{ 
		if(0==j || T[i]==T[j])   
		{ 
			++i; 
			++j; 
			if(T[i]!=T[j]) 
				next[i]=j; 
			else 
			    next[i]=next[j]; 
		} 
		else 
			j=next[j]; 
	} 
} 
int KMP(string s1,string s2,int pos,int *next) 
{ 
	string::iterator p1=s1.begin(); 
	string::iterator p2=s2.begin(); 
    int i=pos; 
	int j=1; 
	while(i<s1.length() && j<s2.length()) 
	{ 
		if(0==j || s1[i]==s2[j]) //j=0:是从子串开始匹配. 
		{ 
			++i; 
			++j; 
		} 
		else 
		{ 
			j=next[j]; 
		} 
	} 
		if(j>=s2.length()) 
			return i-s2.length(); 
		else 
			return 0; 
 
} 
void main() 
{ 
   string s1="93486781634"; 
   string s2="22349659874"; 
   string s; 
   multiply(s1,s2,s); 
   cout<<" "<<s1<<endl<<" "<<s2<<endl<<s; 
}