Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5. int find_nearest_pair(string const& str, char first, char second)
  6. {
  7. int min_distance = str.length();
  8. int pos_first = str.find(first);
  9. while (pos_first != str.npos){
  10. if (int pos_second = str.find(second, pos_first + 1)){
  11. if(pos_second != str.npos){
  12. if(int distance = pos_second - pos_first){
  13. if(distance < min_distance){
  14. min_distance = distance;
  15. }
  16. }
  17. }
  18. }
  19. pos_first = str.find(first, pos_first + 1);
  20. }
  21. return min_distance == str.length() ? -1 : min_distance - 1;
  22. }
  23. int main(){
  24. string napis;
  25. char p,k;
  26. cin>>napis;
  27. cin>>p>>k;
  28. cout<<find_nearest_pair(napis,p,k);
  29. return 0;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement