Pagini recente » Profil usureluflorian | Diferente pentru problema/cartele intre reviziile 13 si 12 | Borderou de evaluare (job #516912) | Rezultatele filtrării | Cod sursa (job #1977951)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
/**
*/
public class Main {
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");
StringBuilder aasd = new StringBuilder();
Iterator<Integer> iterator = asd.iterator();
while (iterator.hasNext()) {
aasd.append(iterator.next());
if (iterator.hasNext()) {
aasd.append(" ");
}
}
out1.println(c);
out1.println(aasd.toString());
} 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;
}
}