Advertisement
cd62131

Search For Given Strings

Dec 13th, 2013
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.16 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.Iterator;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class Searcher {
  10.   public static void main(String[] args) {
  11.     String[] as = {"A", "B", "C", "D", "E"};
  12.     List<String> ls = new ArrayList<String>();
  13.     ls.addAll(Arrays.asList(as));
  14.     System.out.println(ls);
  15.     Scanner in = null;
  16.     try {
  17.       in = new Scanner(System.in);
  18.     }
  19.     catch (Exception e) {
  20.     }
  21.     List<String> qs = new ArrayList<String>();
  22.     while (in.hasNextLine())
  23.       qs.add(Pattern.quote(in.nextLine().trim()));
  24.     StringBuilder sb = new StringBuilder();
  25.     sb.append("(");
  26.     Iterator<String> it = qs.iterator();
  27.     while (it.hasNext()) {
  28.       sb.append(it.next());
  29.       if (it.hasNext()) sb.append("|");
  30.     }
  31.     sb.append(")");
  32.     Pattern query = Pattern.compile(sb.toString());
  33.     int find = 0;
  34.     for (String s: ls) {
  35.       Matcher m = query.matcher(s);
  36.       if(m.find()) {
  37.         find++;
  38.         System.out.println(s);
  39.       }
  40.     }
  41.     System.out.println((double)find / ls.size());
  42.   }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement