Advertisement
Guest User

Untitled

a guest
Nov 12th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. class Subsequence {
  2.  
  3.    public static void main(String[] args)
  4.    {
  5.     BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  6.  
  7.     try{
  8.         String input=br.readLine();
  9.          String res= input.replaceAll("[^a-zA-Z]"," " );
  10.          String[] Alltokens=res.split("\\s+");
  11.  
  12.             int N=Alltokens.length;   //total number of strings in input line
  13.             for(int i=0;i<N;i++)  //Alltokens is the input line
  14.         Alltokens[i]=Alltokens[i].toLowerCase();
  15.  
  16.        int   k= Integer.parseInt(br.readLine());  //number of tokens to be searched in sub sequence of input line
  17.        String[] tokens=new String[k];
  18.        for(int i=0;i<k;i++)
  19.        {tokens[i]=br.readLine();
  20.        tokens[i]=tokens[i].toLowerCase();
  21.        }
  22.  
  23.         boolean found=false;      //Flag to indicate if all tokens have been found
  24.  
  25.       HashSet<String> lookup=new HashSet<String>();
  26.  
  27.       for (int start=0;start<k;start++)
  28.       {//build a HashSet of first k strings of input line.Minimun set which can contain k different tokens
  29.       lookup.add(Alltokens[start]);
  30.       }
  31.  
  32.      Collection<String> tok=Arrays.asList(tokens); //collection of tokens to be found
  33.      System.out.println("tokens are  "+tok.toString());
  34.      if(lookup.containsAll(tok))
  35.       found=true;
  36.  
  37.     int minRange=0,maxRange=N;  //minimun interval which contains all tokens
  38.  
  39.       for(int min=0,max=k;min>=0&&max<N;)  //Start with first string in input,If tokens are not found.Add kth string in input and test
  40.     {
  41.      if(found==false)
  42.        {while(!lookup.containsAll(tok)&&max<N)  //Minimum value of max for which min-max contains tok
  43.         {
  44.          lookup.add(Alltokens[max]);
  45.          System.out.println(" Increasing Max "+lookup.toString()+"  found ="+found);
  46.          max++
  47.          }//close while
  48.  
  49.        if(max<N)
  50.          found=true;
  51.        else
  52.        {found=false;         //if all tokens not found in [0-N)
  53.           System.out.print("No subsegment found");
  54.           System.exit(0);
  55.         }                    
  56.        } //Close outer If found==false
  57.  
  58.    else
  59.     {   //If all tokens were found in 0-max ,strip off string[0] and test and so on for next min which would be 1
  60.     lookup.remove(Alltokens[min]);
  61.     min++;
  62.     if(!lookup.containsAll(tok))  //Strip of min from the range min-max and test min+1 to  max
  63.      found=false;
  64.      System.out.println(" Increasing Min "+lookup.toString()+"  found = "+found);
  65.      }//close else
  66.  
  67.  
  68.   if(max-min<maxRange-minRange&&found==true)
  69.  
  70.  {minRange=min;
  71.  
  72.  maxRange=max;
  73.   }
  74.  
  75.  } //Close for loop
  76.  
  77.   if(found==true)        
  78.   System.out.print(minRange+" "+maxRange);
  79.  } //Close try block
  80. catch(IOException e){}
  81.   }
  82.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement