Cod sursa(job #1977945)

Utilizator Antika89noname Antika89 Data 6 mai 2017 15:34:51
Problema Potrivirea sirurilor Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 2.77 kb
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

/**
 */
public class Strmatch {
    public static void main(String[] args) throws FileNotFoundException {
        Map<Integer, String> integerStringMap = readFromFile();
        char[] firstLine = integerStringMap.get(0).toCharArray();
        char[] secondLine = integerStringMap.get(1).toCharArray();

        int firstIndex = firstLine.length;
        int secondIndex = secondLine.length;
        final Collection<Integer> asd = new ArrayList<Integer>();
        int c =0;
        if (firstIndex <= secondIndex) {
            int i = 0;
            while (i < secondIndex) {
                boolean hasFound = false;

                int j = 0;
                while (j < firstIndex && bl(i, j) && i < secondIndex && firstLine[j] == secondLine[i]) {
                    i++;
                    j++;
                    hasFound = true;
                }

                if (!hasFound) {
                    i++;
                }

                if (j == firstIndex) {
                    c++;
                    asd.add(i - j);
                    --i;
                }
            }

            writeTo(c, asd);
        }
    }

    private static boolean bl(int i, int j) {
        //System.out.println("I:"+i+" J:"+j);
        return true;
    }

    private static void writeTo(int c, Collection<Integer> asd) {
        PrintWriter out1 = null;

        try{
            out1 = new PrintWriter("strmatch.out");
            out1.println(String.valueOf(c));

            StringBuilder aasd = new StringBuilder();
            Iterator<Integer> iterator = asd.iterator();
            while (iterator.hasNext()) {
                aasd.append(iterator.next());

                if (iterator.hasNext()) {
                    aasd.append(" ");
                }
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (null != out1) {
                out1.close();
            }
        }
    }

    private static Map<Integer, String> readFromFile() throws FileNotFoundException {
        Map<Integer, String> map = new HashMap<Integer, String>();
        Scanner fileScanner = null;

        try {
            fileScanner = new Scanner(new FileInputStream("strmatch.in"));
            int i = -1;
            while (fileScanner.hasNext()) {
                map.put(++i, fileScanner.nextLine());
            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {
            if (fileScanner != null)
                fileScanner.close();

        }

        return map;
    }
}