病毒检测问题,无法正确检测,感谢大佬了
#include<stdio.h>#include<stdlib.h>
#include<string.h>
#define max 80
typedef struct {
char ch;
int length;
}SString;
int Index_BF(SString S , SString T )
{
int i = 1, j = 1;
while(i <= S.length - T.length && j <= T.length){
if(S.ch == T.ch ){
i++;
j++;
}else{
i=i-j+2;
j=1;
}
}
if(j > T.length){
return 1;
}else{
return 0;
}
}
int StrAssign(SString *S , char str[])
{
if(!S)
{
return 0;
}
int i;
for(i = 1 ; str != '\0' ; i ++)
{
if(i <= max)
S->ch = str;
else
break;
}
S->length = i-1;
return 1;
}
int Intsstring(SString *S)
{
return S->length = 0;
}
int main(int argc, char** argv) {
char virus;
char dna;
SString DNA,Viru;
Intsstring(&DNA);
Intsstring(&Viru);
int pos=0;
printf("请输入人的DNA:");
scanf("%s",dna);
printf("请输入病毒的DNA:");
scanf("%s",virus);
StrAssign(&DNA,dna);
StrAssign(&Viru,virus);
pos=Index_BF(DNA,Viru);
if(pos != 0){
printf("yes\n");
}else{
printf("no\n");
}
return 0;
} 样例输入???样例输出??? 我改了改,你看行不行
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 80
typedef struct {
char ch;
int length;
}SString;
int Index_BF(SString S , SString T )
{
int i=0,j=0;
while(i <S.length&& j < T.length){
if(S.ch == T.ch ){
i++;
j++;
}else{
i=i-j+2;
j=1;
}
}
if(j == T.length){
return 1;
}else{
return 0;
}
}
int StrAssign(SString *S , char str[])
{
if(!S)
{
return 0;
}
int i;
for(i = 0 ; str != '\0' ; i ++)
{
if(i <= max)
S->ch = str;
else
break;
}
S->length = i;
return 1;
}
int Intsstring(SString *S)
{
return S->length = 0;
}
int main(int argc, char** argv) {
char virus;
char dna;
SString DNA,Viru;
Intsstring(&DNA);
Intsstring(&Viru);
int pos=0;
printf("请输入人的DNA:");
scanf("%s",dna);
printf("请输入病毒的DNA:");
scanf("%s",virus);
StrAssign(&DNA,dna);
StrAssign(&Viru,virus);
pos=Index_BF(DNA,Viru);
if(pos != 0){
printf("yes\n");
}else{
printf("no\n");
}
return 0;
}
页:
[1]