fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. String input = "this is a markov chain, this is not a markov chain";
  11. final int N = 2;
  12. HashMap<String, HashMap<String, Integer>> prefixes = generatePrefixes(input, N);
  13.  
  14.  
  15. }
  16.  
  17. public static HashMap<String, HashMap<String, Integer>> generatePrefixes(String input, int n)
  18. {
  19. HashMap<String, HashMap<String, Integer>> prefixes = new HashMap<String, HashMap<String, Integer>>();
  20. String[] words = input.split(" ");
  21. for (int i = 0; i < words.length-2; i++)
  22. {
  23. HashMap<String, Integer> suffixes = new HashMap<String, Integer>();
  24. suffixes.put(words[i+2], 1);
  25.  
  26. if (prefixes.get(words[i] + words[i+1]).get(words[i+2]) == null)
  27. {
  28. prefixes.put(words[i] + words[i+1], suffixes);
  29. }
  30. else
  31. {
  32. suffixes.put(words[i+2], prefixes.get(words[i] + words[i+1]).get(words[i+2])+1);
  33. prefixes.put(words[i] + words[i+1], suffixes);
  34. }
  35. }
  36. return prefixes;
  37. }
  38.  
  39.  
  40. }
Runtime error #stdin #stdout #stderr 0.09s 320512KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at Ideone.generatePrefixes(Main.java:26)
	at Ideone.main(Main.java:12)