Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.36 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class MinDist {
  4.     public static void main(String[] args) {
  5.         Scanner in = new Scanner(System.in);
  6.         String s = in.nextLine();
  7.         char x = in.next().charAt(0), y = in.next().charAt(0);
  8.         int k=0,min,pozx,pozy;
  9.         int min1=1000000;
  10.         int ch=s.length();
  11.         while (k<ch){
  12.            
  13.                 if (x==s.charAt(k)){
  14.                         pozx=k;
  15.                     k++;
  16.                     while (k<ch && s.charAt(k)!=y){
  17.                
  18.                             if (s.charAt(k)==x)
  19.                             pozx=k;
  20.                          k++;
  21.                     }
  22.             if (k==ch) {
  23.                 break;
  24.             }
  25.                     pozy=k;
  26.                     min=pozy-pozx-1;
  27.                     if (min<min1)
  28.                             min1=min;                
  29.                 }
  30.                 if (y==s.charAt(k)){
  31.                     pozy=k;
  32.                     k++;
  33.                     while (k<ch && s.charAt(k)!=x){
  34.                
  35.                         if (s.charAt(k)==y)
  36.                             pozy=k;
  37.                  k++;
  38.                      }
  39. if (k==ch) {
  40.                 break;
  41.             }
  42.                     pozx=k;
  43.                     min=pozx-pozy-1;
  44.                     if (min<min1)
  45.                             min1=min;
  46.    
  47.                 }
  48.                 k++;
  49.                      }
  50.            if (k==ch)
  51.                         System.out.println(min1);
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement