Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class chomsky {
- public static void main(String[] args) throws IOException {
- Scanner s = new Scanner(new File("chomsky.in"));
- int numCases = s.nextInt();
- for (int z = 1; z <= numCases; z++) {
- try {
- if (z != 1)
- System.out.println();
- System.out.println("Grammar #" + z + ":");
- Grammar grammar = new Grammar();
- int numVariables = s.nextInt();
- boolean epsilon = false;
- for (int v = 0; v < numVariables; v++) {
- int numRules = s.nextInt();
- char variable = s.next().charAt(0);
- for (int r = 0; r < numRules; r++) {
- String rule = s.next();
- if (rule.charAt(0) == '@') {
- epsilon = true;
- } else {
- grammar.AddTransformation(variable, rule);
- }
- }
- }
- int numStrings = s.nextInt();
- for (int i = 0; i < numStrings; i++) {
- String str = s.next();
- if (str.equals("@")) {
- System.out.println(str + ": "
- + (epsilon ? "YES" : "NO"));
- } else {
- System.out.println(str + ": "
- + (grammar.Recognize(str) ? "YES" : "NO"));
- }
- }
- } catch (Exception ex) {}
- }
- }
- static class Grammar {
- private List<Character>[] terminalProductions;
- private List<String>[] variableProductions;
- public Grammar() {
- terminalProductions = new List[26];
- variableProductions = new List[26];
- for (int i = 0; i < 26; i++) {
- terminalProductions[i] = new ArrayList<Character>();
- variableProductions[i] = new ArrayList<String>();
- }
- }
- public void AddTransformation(char from, String to) {
- from = Character.toUpperCase(from);
- if (to.length() == 1)
- terminalProductions[from - 'A'].add(to.charAt(0));
- else
- variableProductions[from - 'A'].add(to);
- }
- public List<Character> GetTerminalProductions(char from) {
- return terminalProductions[Character.toUpperCase(from) - 'A'];
- }
- public List<String> GetVariableProductions(char from) {
- return variableProductions[Character.toUpperCase(from) - 'A'];
- }
- private boolean terminalContains(int j, char contains) {
- for (int a = 0; a < terminalProductions[j].size(); a++) {
- if (terminalProductions[j].get(a).charValue() == contains) {
- return true;
- }
- }
- return false;
- }
- public boolean Recognize(String str) {
- boolean[][][] table = new boolean[str.length() + 1][str.length() + 1][26 + 1];
- for (int i = 1; i <= str.length(); i++) {
- for (int j = 1; j <= 26; j++) {
- if (terminalContains(j - 1, str.charAt(i - 1))) {
- table[1][i][j] = true;
- }
- }
- }
- for (int i = 2; i <= str.length(); i++) { // Length
- for (int j = 1; j <= str.length() - i + 1; j++) { // Start pos
- for (int k = 1; k <= i - 1; k++) { // Partition
- for (int a = 1; a <= 26; a++) {
- for (int p = 0; p < variableProductions[a - 1]
- .size(); p++) {
- int b = variableProductions[a - 1].get(p)
- .charAt(0) - 'A' + 1;
- int c = variableProductions[a - 1].get(p)
- .charAt(1) - 'A' + 1;
- if (table[k][j][b] && table[i - k][j + k][c])
- table[i][j][a] = true;
- }
- }
- }
- }
- }
- return table[str.length()][1]['S' - 'A' + 1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement