Advertisement
jsprieto10

Untitled

Dec 7th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Set;
  5. import java.util.HashSet;
  6.  
  7. public class ProblemaC {
  8.  
  9. private void createSuperString(Set<String> subStrings) {
  10. int totalStrings = subStrings.size();
  11. String[] match = new String[totalStrings];
  12. int i = 0;
  13.  
  14. for(String superString : subStrings) {
  15.  
  16. Set<String> temp = new HashSet<String>(subStrings);
  17. String maxSuperString = superString;
  18. while(temp.size() > 1) {
  19.  
  20. String subString = "";
  21. String nextMaxSuperString = maxSuperString;
  22.  
  23. for(String nextString : temp) {
  24.  
  25. if(!nextString.equals(nextMaxSuperString)) {
  26.  
  27. String superTemp = getSuperString(maxSuperString, nextString);
  28. if (nextMaxSuperString.equals(maxSuperString) || nextMaxSuperString.length() > superTemp.length()) {
  29. nextMaxSuperString = superTemp;
  30. subString = nextString;
  31. }
  32. }
  33. }
  34.  
  35. temp.remove(maxSuperString);
  36. temp.remove(subString);
  37. maxSuperString = nextMaxSuperString;
  38. temp.add(maxSuperString);
  39. }
  40.  
  41. match[i] = maxSuperString;
  42. i++;
  43. }
  44.  
  45. String bestAns = match[0];
  46.  
  47. for(i = 1; i < match.length; i++) {
  48. if(bestAns.length() > match[i].length()) {
  49. bestAns = match[i];
  50. }
  51. }
  52.  
  53. System.out.println("respuesta: "+bestAns);
  54.  
  55. }
  56.  
  57. private String getSuperString(String superString, String someString) {
  58. String result = superString;
  59.  
  60. int endIndex = someString.length() - 1;
  61.  
  62. while(endIndex > 0 && !superString.endsWith(someString.substring(0, endIndex))) {
  63. endIndex--;
  64. }
  65.  
  66. if(endIndex > 0) {
  67. result += someString.substring(endIndex);
  68. }
  69. else {
  70. result += someString;
  71. }
  72.  
  73. return result;
  74. }
  75.  
  76. public static void main(String arg[]) throws IOException {
  77.  
  78. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  79. String lineain;
  80. String data[];
  81. Set<String> fragments;
  82. ProblemaC superStringCreator;
  83.  
  84. int n;
  85. int k;
  86.  
  87.  
  88. while (true){
  89. fragments = new HashSet<String>();
  90. lineain = br.readLine();
  91. if (lineain.isEmpty()) return;
  92. data = lineain.split(" ");
  93. n = Integer.parseInt(data[0]);
  94. k = Integer.parseInt(data[1]);
  95. for (int i = 0; i != n; i++) {
  96. lineain = br.readLine();
  97. fragments.add(lineain);
  98.  
  99. }
  100. superStringCreator = new ProblemaC();
  101. System.out.println("procesando: ");
  102. superStringCreator.createSuperString(fragments);
  103.  
  104. }
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement