Thursday, 1 January 2015

Needle in the haysack (spoj) (solved just by using the c++ string fucntions)

link to question http://www.spoj.com/problems/NHAY/

#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main()
{   int t;
//    scan
    string a,b;
    int f=-1;
    while(cin>>t>>a>>b)
    {
        do{//int k=a.length();
     f=b.find(a,f+1);
     if(f!=-1)
        cout<<f<<endl;}while(f!=-1);
printf("\n");
    }
 
}


The find function returns the first position where the string is found ,
The second parameter allows the substring to be found in the original string after a particular position , and since we want the next substring to be found after the found result , be initialize f with -1 and always search with f+1 ,
if the sunstring is not found , the find function returns -1 .
:)