Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public static Vector<Integer> solveVignereII(String cipherText) {
  2. Vector<Integer> ret = new Vector<Integer>();
  3.  
  4. Map<String, Integer> tlas = new HashMap<String, Integer>();
  5.  
  6. // find frequency of TLAs
  7. for (int i : new Range(cipherText.length() - 3)) {
  8. String tla = cipherText.substring(i, i + 3);
  9. if (tlas.containsKey(tla)) {
  10. tlas.put(tla, tlas.get(tla) + 1);
  11. } else {
  12. tlas.put(tla, 1);
  13. }
  14. }
  15. puts(tlas);
  16.  
  17. Map<Integer, Integer> dists = new HashMap<Integer, Integer>();
  18.  
  19. for (String k : tlas.keySet()) {
  20. if (tlas.get(k) < 2) {
  21. // tlas.remove(k);
  22. } else {
  23. int i = cipherText.indexOf(k);
  24. for (int n = cipherText.indexOf(k, i + 1);
  25. n > 0;
  26. i = n, n = cipherText.indexOf(k, n + 1)) {
  27. int dist = n - i;
  28. if (dists.containsKey(dist)) {
  29. dists.put(dist, dists.get(dist) + 1);
  30. } else {
  31. dists.put(dist, 1);
  32. }
  33. }
  34. }
  35. }
  36. puts(dists);
  37.  
  38. for (int i : new Range(4, 21)) {
  39. int matchrate = 0;
  40. int total = 0;
  41. for (int d : dists.keySet()) {
  42. if (d % i == 0) {
  43. matchrate += dists.get(d);
  44. }
  45. total += dists.get(d);
  46. }
  47. puts("" + i + " " + matchrate + "/" + dists.size());
  48. if (matchrate > total * .9) {
  49. ret.add(i);
  50. }
  51. }
  52. puts(ret);
  53.  
  54. return ret;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement