Hi, I'm having problems with this problem, my code is: 

import java.io.*;
import java.util.Scanner;

public class Solution {

    public static void main(String[] args) throws FileNotFoundException {
        // File file = new 
File("/Users/berendjanlange/GitDrive/Hashcode/CodeJamQualification/src/scratch.txt");
        // Scanner in = new Scanner(new BufferedReader(new 
FileReader(file)));
        Scanner in = new Scanner(new BufferedReader(new 
InputStreamReader(System.in)));
        int tests = in.nextInt();

        nextTest: for (int test = 0; test < tests; test++) {
            int no_patterns = in.nextInt();
            String beginName = "";
            String endName = "";
            String middleName = "";
            int Case = test + 1;

            for (int pat = 0; pat < no_patterns; pat++) {

                String pattern = in.next();

                String[] pattern_split = pattern.split("\\*", -1);
                
                String next_beginName = pattern_split[0];
                String next_endName = pattern_split[pattern_split.length - 
1];
                

                // Matches the middle part of the name //
                for (int i = 1; i < pattern_split.length - 1; i++) {
                    middleName += pattern_split[i];
                }

                // Matches the beginning part of the name by checking if 
the substring is indexOf at position 0 //
                if (beginName.length() == 0) {
                    beginName = next_beginName;
                }
                int index_string = beginName.indexOf(next_beginName);
                if (index_string < 0) {
                    index_string = next_beginName.indexOf(beginName);
                    if (index_string == 0) {
                        beginName = next_beginName;
                    } else {
                        System.out.println("Case #" + Case + ": *");
                        continue nextTest;
                    }
                } else if (index_string > 0) {
                    System.out.println("Case #" + Case + ": *");
                    continue nextTest;
                }

                // Matches the end part of the name //
                if (endName.length() == 0) {
                    endName = next_endName;
                }
                // Matches the end part of the name by reversing it and 
applying same checking as for begin of name //
                String endNameReverse = new 
StringBuilder(endName).reverse().toString();
                String next_endNameReverse = new 
StringBuilder(next_endName).reverse().toString();
                index_string = endNameReverse.indexOf(next_endNameReverse);
                if (index_string < 0) {
                    index_string = 
next_endNameReverse.indexOf(endNameReverse);
                    if (index_string == 0) {
                        endName = next_endName;
                    } else {
                        System.out.println("Case #" + Case + ": *");
                        continue nextTest;
                    }
                }
                else if (index_string > 0) {
                    System.out.println("Case #" + Case + ": *");
                    continue nextTest;
                }
            }
            System.out.println("Case #" + Case + ": " + beginName + 
middleName + endName);
        }
    }
}


-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/e6b6f64e-2b9c-4b8e-b13b-2c0163e0ec1a%40googlegroups.com.

Reply via email to