|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<iostream>
using namespace std;
#include"c.h"
int main()
{
sqstr s1, t1, str;
input(s1, "dasczxca");
input(t1, "dascxzfe");
cout<<"s1: ";
output(s1);
cout<<"t1: ";
output(t1);
str = maxstr(s1, t1);
cout<<"str: "; output(str);
cout<<str.length<<endl;
return 0;
}
#define MaxSize 100
typedef struct
{
char data[MaxSize];
int length;
}sqstr;
void input(sqstr &s, char str[])
{
int i = 0;
while(str[i] != '\0')
{
s.data[i] = str[i];
i++;
}
s.length = i;
}
void output(sqstr s)
{
int i;
for(i=0; i < s.length; i++)
{
cout<<s.data[i];
cout<<endl;
}
}
sqstr maxstr(sqstr s, sqstr t)
{
sqstr str;
int midx = 0, mlen = 0, tlen, i = 0, j, k;
while(i < s.length)
{
j = 0;
while(j < t.length)
{
if(s.data[i] == t.data[j])
{
tlen = 1;
for(k=1; i+k < s.length && j+k < t.length && s.data[i+k] == t.data[j+k]; k++)
{
tlen++;
}
if(tlen > mlen)
{
midx = i;
mlen = tlen;
}
j = j+1;
}
else j++;
}
i++;
}
for(i=0; i < mlen; i++)
{
str.data[i] = s.data[midx+i];
}
str.length = mlen;
return str;
}
输出结果:
s1: d
a
s
c
z
x
c
a
t1: d
a
s
c
x
z
f
e
str: d
a
s
c
4
Press any key to continue
|
|