Advertisement
Alisator

PAL5_4-12_easyTree

Dec 4th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.49 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.math.BigInteger;
  8. import java.util.Stack;
  9. import java.util.StringTokenizer;
  10.  
  11. public class Main {
  12.  
  13.     public static String alphabet;
  14.     public static int acceptThreshold;
  15.     public static int wordlength;
  16.     public static int[] charmap = new int[256];
  17.     public static BigInteger alphabetSize;
  18.  
  19.     public static void main(String[] args) throws IOException {
  20.         args = new String[]{"pub10.in"};
  21.         BufferedReader br = (args.length == 0)
  22.                 ? new BufferedReader(new InputStreamReader(System.in))
  23.                 : new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
  24.         //string f = "pub09.in";
  25.  
  26.         StringTokenizer configuration = new StringTokenizer(br.readLine());
  27.         String alphabet = configuration.nextToken();
  28.         alphabetSize = BigInteger.valueOf(alphabet.length());
  29.           for (int i = 0; i < alphabet.length(); i++) {
  30.             int c = alphabet.indexOf(i);
  31.         //c++, jinak vracelo -1
  32.             charmap[c+1] = i;
  33.         }
  34.         int membersCnt = Integer.parseInt(configuration.nextToken());
  35.         acceptThreshold = Integer.parseInt(configuration.nextToken());
  36.         wordlength = Integer.parseInt(configuration.nextToken());
  37.  
  38.  
  39. //int cannot be dereferenced
  40.             Node FullTree = new Node(-1, 0, 0);
  41.  
  42.         int set = 0;
  43.         while (br.readLine() != null) {
  44.             set++;
  45.             int countOfProp = Integer.parseInt(br.readLine());
  46.             for (int i = 0; i < countOfProp; i++) {
  47.                 Node.Mark2(FullTree, MapWord(br.readLine()), 0, set);
  48.             }
  49.         }
  50.         BigInteger count = BigInteger.ZERO;
  51.         Stack<Node> ns = new Stack<>();
  52.         ns.push(FullTree);
  53.         while (ns.size() > 0) {
  54.             count = count.add(Node.FindCnt2(ns).mod(BigInteger.valueOf(100000)));
  55.         }
  56.         System.out.println(count);
  57.     }
  58.  
  59.     public static int[] MapWord(String word) {
  60.         int[] res = new int[word.length()];
  61.         for (int i = 0; i < res.length; i++) {
  62.             res[i] = charmap[word.indexOf(i)];
  63.         }
  64.         return res;
  65.     }
  66.  
  67.     public static class Node {
  68.  
  69.         int AcceptCnt;
  70.         int Index;
  71.         public Node[] Children;
  72.         int SetToken = -100;
  73.         int Depth = 0;
  74.  
  75.         public Node(int c, int acceptcnt, int depth) {
  76.             AcceptCnt = acceptcnt;
  77. //null pointer exception
  78.             Children = new Node[alphabet.length()];
  79.             Index = c;
  80.             Depth = depth;
  81.         }
  82.  
  83.         public static void Mark2(Node cnode, int[] word, int index, int setToken) {
  84.             while (cnode.AcceptCnt < acceptThreshold && cnode.SetToken != setToken) {
  85.                 if (index >= word.length)//end
  86.                 {
  87.                     cnode.SetToken = setToken;
  88.                     Stack<Node> ns = new Stack<>();
  89.                     ns.add(cnode);
  90.                     while (ns.size() > 0) {
  91.                         IncrementMark2(ns);
  92.                     }
  93.                     return;
  94.                 }
  95.  
  96.                 int i = word[index];
  97.                 if (cnode.Children[i] == null) {
  98.                     cnode.Children[i] = new Node(i, cnode.AcceptCnt, index + 1);
  99.                 }
  100.  
  101.                 cnode = cnode.Children[i];
  102.                 index++;
  103.             }
  104.  
  105.         }
  106.  
  107.         public static void IncrementMark2(Stack<Node> nstack) {
  108.             Node n = nstack.pop();
  109.             n.AcceptCnt++;
  110.             for (Node Children1 : n.Children) {
  111.                 if (Children1 != null) {
  112.                     nstack.push(Children1);
  113.                 }
  114.             }
  115.  
  116.         }
  117.  
  118.         public static BigInteger FindCnt2(Stack<Node> ns) {
  119.             Node node = ns.pop();
  120.             if (node.AcceptCnt >= acceptThreshold) {
  121.                 //return BigInteger.ModPow(Program.alphabet.length(), (Program.wordlength - node.Depth), 100000);
  122.         // se to musi predelat do foru pro multiply
  123.                 BigInteger.ONE.multiply(
  124.                         alphabetSize.pow(wordlength - node.Depth).mod(BigInteger.valueOf(10000))
  125.                 );
  126.             }
  127.  
  128.             BigInteger cnt = BigInteger.ZERO;
  129.             for (Node Children1 : node.Children) {
  130.                 if (Children1 != null) {
  131.                     ns.push(Children1);
  132.                 }
  133.             }
  134.  
  135.             return BigInteger.ZERO;
  136.         }
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement