33,008
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<cstring>
using namespace std;
const int MM=100000;
int next[MM];
char str[MM];
void GetNext(char *t){
int len=strlen(t);
next[0]=-1;
int i=0,j=next[i];
while(i<len){
if(j==-1 || t[i]==t[j]){
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int main(){
int index,len;
while(cin>>str){
char *s=str;
len=0;
for(int i=0;*s!='\0';s++,i++){
GetNext(s);
for(int j=1;j<=strlen(s);j++)
if(next[j]>len){
len=next[j];
index=i+j; //index是第一个最长重复串在str中的位置
}
}
if(len>0){
for(int i=index-len;i<index;i++)
cout<<str[i];
cout<<endl;
}
else
cout<<"none"<<endl;
}
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int lcs(string A,string B);
int c[100][100]={0};
int b[100][100]={0};
void image(int i,int j,string &A);
int main()
{
string a,b;
cin>>a>>b;
if(a.length()>100 || b.length()>100)
{
cerr<<"Can not get the lcs of the string which length over 100"<<endl;
exit(0);
}
string &aa=a;
cout<<"\n"<<endl;
lcs(a,b);
cout<<"\n"<<endl;
image(a.length(),b.length(),aa);
cout<<endl;
system("pause");
return 0;
}
int lcs(string A,string B)
{
int mA=A.length();
int nB=B.length();
int len=0;
for(int i=1;i<=mA;++i)
{
for(int j=1;j<=nB;++j)
{
if(A.at(i-1)==B.at(j-1))
{
c[i][j]=c[i-1][j-1]+1;
len++;
b[i][j]=3;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]=1;
}
else
{
c[i][j]=c[i][j-1];
b[i][j]=2;
}
}
}
//-----image the road
for(int j=1;j<=nB;++j)
{
cout<<"\t"<<B.at(j-1);
}
cout<<endl;
for(int i=1;i<=mA;++i)
{
cout<<A.at(i-1)<<"\t";
for(int j=1;j<=nB;++j)
{
cout<<b[i][j]<<"\t";
}
cout<<"\n"<<endl;
}
return len;
}
void image(int i,int j,string &A)
{
//-----3:up and left,1:up,2:left
if(i==0||j==0)
return;
if (b[i][j]==3)
{
cout<<A.at(i-1)<<"\t";
image(i-1,j-1,A);
}
if(b[i][j]==1)
image(i-1,j,A);
if(b[i][j]==2)
image(i,j-1,A);
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXCHAR 5000 //最长处理5000个字符
char c[MAXCHAR], *a[MAXCHAR];
int comlen( char *p, char *q ){
int i = 0;
while( *p && (*p++ == *q++) )
++i;
return i;
}
int pstrcmp( const void *p1, const void *p2 ){
return strcmp( *(char* const *)p1, *(char* const*)p2 );
}
int main( ){
char ch;
int n=0;
int i, temp;
int maxlen=0, maxi=0;
printf("Please input your string:\n");
while( (ch=getchar())!='\n' ){
a[n]=&c[n];
c[n++]=ch;
}
c[n]='\0';
qsort( a, n, sizeof(char*), pstrcmp );
for(i=0; i<n-1; ++i ){
temp=comlen( a[i], a[i+1] );
if( temp>maxlen ){
maxlen=temp;
maxi=i;
}
}
printf("%.*s\n",maxlen, a[maxi]);
system("PAUSE");
return 0;
}