Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pal;
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.math.BigInteger;
- import java.util.Stack;
- import java.util.StringTokenizer;
- public class Main {
- public static String alphabet;
- public static int acceptThreshold;
- public static int wordlength;
- public static int[] charmap = new int[256];
- public static BigInteger alphabetSize;
- public static void main(String[] args) throws IOException {
- args = new String[]{"pub10.in"};
- BufferedReader br = (args.length == 0)
- ? new BufferedReader(new InputStreamReader(System.in))
- : new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
- //string f = "pub09.in";
- StringTokenizer configuration = new StringTokenizer(br.readLine());
- String alphabet = configuration.nextToken();
- alphabetSize = BigInteger.valueOf(alphabet.length());
- for (int i = 0; i < alphabet.length(); i++) {
- int c = alphabet.indexOf(i);
- //c++, jinak vracelo -1
- charmap[c+1] = i;
- }
- int membersCnt = Integer.parseInt(configuration.nextToken());
- acceptThreshold = Integer.parseInt(configuration.nextToken());
- wordlength = Integer.parseInt(configuration.nextToken());
- //int cannot be dereferenced
- Node FullTree = new Node(-1, 0, 0);
- int set = 0;
- while (br.readLine() != null) {
- set++;
- int countOfProp = Integer.parseInt(br.readLine());
- for (int i = 0; i < countOfProp; i++) {
- Node.Mark2(FullTree, MapWord(br.readLine()), 0, set);
- }
- }
- BigInteger count = BigInteger.ZERO;
- Stack<Node> ns = new Stack<>();
- ns.push(FullTree);
- while (ns.size() > 0) {
- count = count.add(Node.FindCnt2(ns).mod(BigInteger.valueOf(100000)));
- }
- System.out.println(count);
- }
- public static int[] MapWord(String word) {
- int[] res = new int[word.length()];
- for (int i = 0; i < res.length; i++) {
- res[i] = charmap[word.indexOf(i)];
- }
- return res;
- }
- public static class Node {
- int AcceptCnt;
- int Index;
- public Node[] Children;
- int SetToken = -100;
- int Depth = 0;
- public Node(int c, int acceptcnt, int depth) {
- AcceptCnt = acceptcnt;
- //null pointer exception
- Children = new Node[alphabet.length()];
- Index = c;
- Depth = depth;
- }
- public static void Mark2(Node cnode, int[] word, int index, int setToken) {
- while (cnode.AcceptCnt < acceptThreshold && cnode.SetToken != setToken) {
- if (index >= word.length)//end
- {
- cnode.SetToken = setToken;
- Stack<Node> ns = new Stack<>();
- ns.add(cnode);
- while (ns.size() > 0) {
- IncrementMark2(ns);
- }
- return;
- }
- int i = word[index];
- if (cnode.Children[i] == null) {
- cnode.Children[i] = new Node(i, cnode.AcceptCnt, index + 1);
- }
- cnode = cnode.Children[i];
- index++;
- }
- }
- public static void IncrementMark2(Stack<Node> nstack) {
- Node n = nstack.pop();
- n.AcceptCnt++;
- for (Node Children1 : n.Children) {
- if (Children1 != null) {
- nstack.push(Children1);
- }
- }
- }
- public static BigInteger FindCnt2(Stack<Node> ns) {
- Node node = ns.pop();
- if (node.AcceptCnt >= acceptThreshold) {
- //return BigInteger.ModPow(Program.alphabet.length(), (Program.wordlength - node.Depth), 100000);
- // se to musi predelat do foru pro multiply
- BigInteger.ONE.multiply(
- alphabetSize.pow(wordlength - node.Depth).mod(BigInteger.valueOf(10000))
- );
- }
- BigInteger cnt = BigInteger.ZERO;
- for (Node Children1 : node.Children) {
- if (Children1 != null) {
- ns.push(Children1);
- }
- }
- return BigInteger.ZERO;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement