Guest User

Untitled

a guest
May 27th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main {
  4.  
  5. private static int saveTheUniverse(ArrayList<String> searchEngines, ArrayList<String> searches){
  6.  
  7. // Keeps a list of the search engines that have been used for each loop
  8. String[] searchEnginesUsed = new String[searchEngines.size()];
  9. int numOfEnginesUsed = 0;
  10. int changes = 0;
  11.  
  12. // Progress through the given list of search terms
  13. for(int i =0; i < searches.size(); i++){
  14. boolean usedAlready = false;
  15.  
  16. // Check if the current search term has been used before
  17. for(String s : searchEnginesUsed){
  18. if (searches.get(i).equals(s)){
  19. usedAlready = true;
  20. }
  21. }
  22.  
  23. // if the search term has not been used before,
  24. // add it to the list of used search engines
  25. // and increment the numOfEnginesUsed by one
  26. if (usedAlready == false){
  27. searchEnginesUsed[numOfEnginesUsed] = searches.get(i);
  28. numOfEnginesUsed ++;
  29. // if all search engines have been searched we can assume a change was neccesary
  30. if (numOfEnginesUsed == searchEnginesUsed.length){
  31. // Move back one position in the list because
  32. // a search engine can not search itself
  33. i--;
  34. changes ++;
  35. numOfEnginesUsed = 0;
  36.  
  37. // Reset the list of searches used
  38. for(int j = 0; j < searchEnginesUsed.length; j++){
  39. searchEnginesUsed[j] = "";
  40. }
  41. }
  42.  
  43. }
  44. }
  45.  
  46. return changes;
  47. }
  48.  
  49. public static void main(String[] args) {
  50. Scanner scanner = new Scanner(System.in);
  51.  
  52. int testCases = scanner.nextInt();
  53.  
  54.  
  55. for(int i = 0; i < testCases; i++ ){
  56. ArrayList<String> searchEngines = new ArrayList<String>();
  57. ArrayList<String> searches = new ArrayList<String>();
  58.  
  59. int numSearchEngines = scanner.nextInt();
  60. scanner.nextLine();
  61.  
  62. for(int j = 0; j < numSearchEngines; j++){
  63. searchEngines.add(scanner.nextLine());
  64. }
  65. int numSearches = scanner.nextInt();
  66. scanner.nextLine();
  67.  
  68. for(int j = 0; j < numSearches; j++){
  69. searches.add(scanner.nextLine());
  70. }
  71.  
  72. System.out.println("Case #" + (i+1) + ": " +saveTheUniverse(searchEngines,searches));
  73. }
  74. }
  75. }
Add Comment
Please, Sign In to add comment