Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class chomsky {
  4. public static void main(String[] args) throws IOException {
  5. Scanner s = new Scanner(new File("chomsky.in"));
  6. int numCases = s.nextInt();
  7. for (int z = 1; z <= numCases; z++) {
  8. try {
  9. if (z != 1)
  10. System.out.println();
  11. System.out.println("Grammar #" + z + ":");
  12. Grammar grammar = new Grammar();
  13. int numVariables = s.nextInt();
  14. boolean epsilon = false;
  15.  
  16. for (int v = 0; v < numVariables; v++) {
  17. int numRules = s.nextInt();
  18. char variable = s.next().charAt(0);
  19. for (int r = 0; r < numRules; r++) {
  20. String rule = s.next();
  21. if (rule.charAt(0) == '@') {
  22. epsilon = true;
  23. } else {
  24. grammar.AddTransformation(variable, rule);
  25. }
  26. }
  27. }
  28. int numStrings = s.nextInt();
  29. for (int i = 0; i < numStrings; i++) {
  30. String str = s.next();
  31. if (str.equals("@")) {
  32. System.out.println(str + ": "
  33. + (epsilon ? "YES" : "NO"));
  34. } else {
  35. System.out.println(str + ": "
  36. + (grammar.Recognize(str) ? "YES" : "NO"));
  37. }
  38. }
  39. } catch (Exception ex) {}
  40. }
  41.  
  42. }
  43.  
  44. static class Grammar {
  45. private List<Character>[] terminalProductions;
  46. private List<String>[] variableProductions;
  47.  
  48. public Grammar() {
  49. terminalProductions = new List[26];
  50. variableProductions = new List[26];
  51. for (int i = 0; i < 26; i++) {
  52. terminalProductions[i] = new ArrayList<Character>();
  53. variableProductions[i] = new ArrayList<String>();
  54. }
  55. }
  56.  
  57. public void AddTransformation(char from, String to) {
  58. from = Character.toUpperCase(from);
  59. if (to.length() == 1)
  60. terminalProductions[from - 'A'].add(to.charAt(0));
  61. else
  62. variableProductions[from - 'A'].add(to);
  63. }
  64.  
  65. public List<Character> GetTerminalProductions(char from) {
  66. return terminalProductions[Character.toUpperCase(from) - 'A'];
  67. }
  68.  
  69. public List<String> GetVariableProductions(char from) {
  70. return variableProductions[Character.toUpperCase(from) - 'A'];
  71. }
  72.  
  73. private boolean terminalContains(int j, char contains) {
  74. for (int a = 0; a < terminalProductions[j].size(); a++) {
  75. if (terminalProductions[j].get(a).charValue() == contains) {
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81.  
  82. public boolean Recognize(String str) {
  83. boolean[][][] table = new boolean[str.length() + 1][str.length() + 1][26 + 1];
  84. for (int i = 1; i <= str.length(); i++) {
  85. for (int j = 1; j <= 26; j++) {
  86. if (terminalContains(j - 1, str.charAt(i - 1))) {
  87. table[1][i][j] = true;
  88. }
  89. }
  90. }
  91. for (int i = 2; i <= str.length(); i++) { // Length
  92. for (int j = 1; j <= str.length() - i + 1; j++) { // Start pos
  93. for (int k = 1; k <= i - 1; k++) { // Partition
  94. for (int a = 1; a <= 26; a++) {
  95. for (int p = 0; p < variableProductions[a - 1]
  96. .size(); p++) {
  97. int b = variableProductions[a - 1].get(p)
  98. .charAt(0) - 'A' + 1;
  99. int c = variableProductions[a - 1].get(p)
  100. .charAt(1) - 'A' + 1;
  101. if (table[k][j][b] && table[i - k][j + k][c])
  102. table[i][j][a] = true;
  103. }
  104. }
  105. }
  106. }
  107. }
  108. return table[str.length()][1]['S' - 'A' + 1];
  109. }
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement