Viết chương trình xây dựng hàm tìm vị trí chuỗi con trong chuỗi mẹ
Code:
#include <bits/stdc++.h>
using namespace std;
int Compare(char *s = new char[1000] , char *r = new char[1000])
{
int j = 0;
for(int i = 0 ; i < strlen(s) ; i++)
{
if(*(s+i) == *(r+i))
j++;
if(*(s+i) != *(r+i))
j--;
}
if(j == strlen(r))
return 1;
return 0;
}
int Count_string(char *s = new char[1000],char * temp = new char[1000])
{
char a[100000];
int x=0,f=0,g=0,count=-1,gg=0;
for(int i = 0 ; i < strlen(s) ; i++)
{
if(f == 0 && *(s+i) != ' ')
f = 1;
if(f != 0)
{
if(*(s+i) == ' ')
g++;
if(g <= 1)
{
a[x++] = *(s+i);
if(*(s+i+1) != ' ')
g=0;
if(*(s+i) == ' ')
{
count++;
int j = 0;
for(int i = 0 ; i < strlen(temp) ; i++)
{
if(*(a+i) == *(temp+i))
j++;
if(*(a+i) != *(temp+i))
j--;
}
if(j == strlen(temp))
{
return count;
break;
}
x = 0;
}
}
if(g > 1 && *(s+i+1) != ' ')
g = 0;
}
gg++;
}
if(gg == strlen(s))
count++;
return count;
}
int main()
{
char *s = new char[1000],*temp = new char[1000];
gets(s);
fflush(stdin);
gets(temp);
cout<<Count_string(s,temp);
return 0;
}