Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Set;
- import java.util.HashSet;
- public class ProblemaC {
- private void createSuperString(Set<String> subStrings) {
- int totalStrings = subStrings.size();
- String[] match = new String[totalStrings];
- int i = 0;
- for(String superString : subStrings) {
- Set<String> temp = new HashSet<String>(subStrings);
- String maxSuperString = superString;
- while(temp.size() > 1) {
- String subString = "";
- String nextMaxSuperString = maxSuperString;
- for(String nextString : temp) {
- if(!nextString.equals(nextMaxSuperString)) {
- String superTemp = getSuperString(maxSuperString, nextString);
- if (nextMaxSuperString.equals(maxSuperString) || nextMaxSuperString.length() > superTemp.length()) {
- nextMaxSuperString = superTemp;
- subString = nextString;
- }
- }
- }
- temp.remove(maxSuperString);
- temp.remove(subString);
- maxSuperString = nextMaxSuperString;
- temp.add(maxSuperString);
- }
- match[i] = maxSuperString;
- i++;
- }
- String bestAns = match[0];
- for(i = 1; i < match.length; i++) {
- if(bestAns.length() > match[i].length()) {
- bestAns = match[i];
- }
- }
- System.out.println("respuesta: "+bestAns);
- }
- private String getSuperString(String superString, String someString) {
- String result = superString;
- int endIndex = someString.length() - 1;
- while(endIndex > 0 && !superString.endsWith(someString.substring(0, endIndex))) {
- endIndex--;
- }
- if(endIndex > 0) {
- result += someString.substring(endIndex);
- }
- else {
- result += someString;
- }
- return result;
- }
- public static void main(String arg[]) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- String lineain;
- String data[];
- Set<String> fragments;
- ProblemaC superStringCreator;
- int n;
- int k;
- while (true){
- fragments = new HashSet<String>();
- lineain = br.readLine();
- if (lineain.isEmpty()) return;
- data = lineain.split(" ");
- n = Integer.parseInt(data[0]);
- k = Integer.parseInt(data[1]);
- for (int i = 0; i != n; i++) {
- lineain = br.readLine();
- fragments.add(lineain);
- }
- superStringCreator = new ProblemaC();
- System.out.println("procesando: ");
- superStringCreator.createSuperString(fragments);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement