fork download
  1. #include <iostream>
  2. #include <set>
  3.  
  4.  
  5. bool find_in_string( int pos, std::string s, std::string str)
  6. {
  7. std::string str_s = str.substr( pos, s.length());
  8. int s_pos = 0;
  9.  
  10. while( !s.empty())
  11. {
  12. std::size_t found = str_s.find( s[0]);
  13. if ( found!=std::string::npos)
  14. {
  15. s.erase( 0, 1);
  16. str_s.erase( found, 1);
  17. } else {
  18. return 0;
  19. }
  20.  
  21. }
  22.  
  23. return 1;
  24. }
  25.  
  26. bool find_in_string( std::string s, std::string str)
  27. {
  28. bool found = false;
  29. int pos = 0;
  30.  
  31. while( !found && pos < str.length() - s.length() + 1)
  32. {
  33. found = find_in_string( pos++, s, str);
  34. }
  35.  
  36. return found;
  37. }
  38.  
  39. int main() {
  40.  
  41. std::string s1 = "abcdpqrs";
  42. std::string s2 = "adcbpqrs";
  43. std::string searched = "dcb";
  44. std::string searched2 = "pdq";
  45. std::string searched3 = "bcpq";
  46. std::cout << find_in_string( searched, s1);
  47. std::cout << find_in_string( searched, s2);
  48. std::cout << find_in_string( searched2, s1);
  49. std::cout << find_in_string( searched3, s1);
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
1110