Search Pattern
PROBLEM :
Given two strings, one is a text string and other is a pattern string. The task is to print the indexes of all the occurences of pattern string in the text string. For printing, Starting Index of a string should be taken as 1.
Note: Strings contain only lower case alphabets.
Input:
First line of the input contains an integer 'T' denoting the total number of test cases. Then T test cases follow. Each test consists of two lines. First line of each test case contains the text string. Second line of each test case contains the pattern string.
Output:
Print indexes all the occurences of the pattern strings in the text string in a single line seprated by spaces.
Print -1 if no pattern found.
Constraints:
1 <= T <= 100
1 <= Sizeof of text String <= 10000
1 <= Sizeof pattern String <= Sizeof text String
Example:
Input :
2
batmanandrobinarebatfriends
bat
abcsdu
edu
Output :
1 18
-1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
void Replace_by_X(string ,string ) ;
int compare(string ,string ,int ) ;
int main()
{
int t ;
string str,patt ;
cin>>t ;
while(t--)
{
cin>>str>>patt ;
Replace_by_X(str,patt) ;
cout<<endl ;
}
return 0;
}
void Replace_by_X(string str,string patt)
{
string ans ;
int l1,l2,i ;
bool flag=true ;
l1=str.length() ;
l2=patt.length() ;
for(i=0;i<l1;i++)
{
if((str[i]==patt[0])&&(compare(str,patt,i)))
{
cout<<i+1<<" " ;
flag=false ;
}
}
if(flag==true)
cout<<-1 ;
}
int compare(string str,string patt,int pos)
{
int l=patt.length();
int i ;
for(i=0;i<l;i++)
{
if(patt[i]!=str[pos+i])
return 0 ;
}
return 1 ;
}
---------------------------------------------------------------------------------
Given two strings, one is a text string and other is a pattern string. The task is to print the indexes of all the occurences of pattern string in the text string. For printing, Starting Index of a string should be taken as 1.
Note: Strings contain only lower case alphabets.
Input:
First line of the input contains an integer 'T' denoting the total number of test cases. Then T test cases follow. Each test consists of two lines. First line of each test case contains the text string. Second line of each test case contains the pattern string.
Output:
Print indexes all the occurences of the pattern strings in the text string in a single line seprated by spaces.
Print -1 if no pattern found.
Constraints:
1 <= T <= 100
1 <= Sizeof of text String <= 10000
1 <= Sizeof pattern String <= Sizeof text String
Example:
Input :
2
batmanandrobinarebatfriends
bat
abcsdu
edu
Output :
1 18
-1
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#include<string.h>
void Replace_by_X(string ,string ) ;
int compare(string ,string ,int ) ;
int main()
{
int t ;
string str,patt ;
cin>>t ;
while(t--)
{
cin>>str>>patt ;
Replace_by_X(str,patt) ;
cout<<endl ;
}
return 0;
}
void Replace_by_X(string str,string patt)
{
string ans ;
int l1,l2,i ;
bool flag=true ;
l1=str.length() ;
l2=patt.length() ;
for(i=0;i<l1;i++)
{
if((str[i]==patt[0])&&(compare(str,patt,i)))
{
cout<<i+1<<" " ;
flag=false ;
}
}
if(flag==true)
cout<<-1 ;
}
int compare(string str,string patt,int pos)
{
int l=patt.length();
int i ;
for(i=0;i<l;i++)
{
if(patt[i]!=str[pos+i])
return 0 ;
}
return 1 ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment