Advertisement
Alisator

pal5_STROM

Dec 3rd, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.20 KB | None | 0 0
  1. package pal;
  2.  
  3. import java.util.*;
  4. import java.math.*;
  5. import java.io.*;
  6.  
  7. /**
  8.  *
  9.  * @author zipec
  10.  * @date 2.12.2014
  11.  */
  12. public class Main {
  13.     public static final int MAX_PROPOSAL = 100000;
  14.    
  15.     public static void main(String[] args) throws IOException {
  16.         //args = new String[]{"pub07.in"};
  17.        
  18.         BufferedReader br = (args.length == 0)
  19.                 ? new BufferedReader(new InputStreamReader(System.in))
  20.                 : new BufferedReader(new InputStreamReader(new FileInputStream(args[0])));
  21.        
  22.         //BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  23.        
  24.         StringTokenizer configuration = new StringTokenizer(br.readLine());
  25.         String alphabet = configuration.nextToken();
  26.         BigInteger alphabetSize = BigInteger.valueOf(alphabet.length());
  27.         int membersCount = Integer.parseInt(configuration.nextToken());
  28.         int acceptThreshold = Integer.parseInt(configuration.nextToken());
  29.         int proposalLength = Integer.parseInt(configuration.nextToken());
  30.        
  31.         Node tree = new Node();
  32.        
  33.         //build tree
  34.         for (int i = 0; i < membersCount; i++) {
  35.             int memberCriteriaCount = Integer.parseInt(br.readLine());
  36.             String[] defaultMemberCriteria = new String[memberCriteriaCount];
  37.             for (int j = 0; j < memberCriteriaCount; j++) {
  38.                 defaultMemberCriteria[j] = br.readLine();
  39.             }
  40.             criteriaLoop:
  41.             for (String criteria : defaultMemberCriteria) { //for all criterias
  42.                 for (String criteriaFilter : defaultMemberCriteria) { //chech against other criterias
  43.                     if (criteriaFilter.length() < criteria.length() && criteria.startsWith(criteriaFilter)) //has prefix criteria
  44.                         continue criteriaLoop; //skips tree.expand
  45.                 }
  46.                 tree.expandStack(criteria, i); //expand tree with current criteria for current member
  47.             }
  48.         }
  49.        
  50.         //collection of results
  51.         //  value is depth in tree
  52.         Collection<Integer> results = tree.complyStack(acceptThreshold);
  53.        
  54.         //aggregate of results
  55.         BigInteger result = countPossibilitiesFromPrefixes(results, alphabetSize, proposalLength);
  56.                
  57.         System.out.println(result.mod(BigInteger.valueOf(MAX_PROPOSAL)));
  58.     }
  59.    
  60.     /***************************************************************************
  61.      * Vrátí součet možných řešení které lze vygenerovat z prefixů v předané množině
  62.      * @param prefixDepths
  63.      * @param alphabetSize
  64.      * @param proposalLength
  65.      * @return
  66.      */
  67.     private static BigInteger countPossibilitiesFromPrefixes(Collection<Integer> prefixDepths, BigInteger alphabetSize, int proposalLength) {
  68.         BigInteger result = BigInteger.ZERO;
  69.         for (int prefixDepth : prefixDepths) {
  70.             result = result.add(
  71.                     BigInteger.ONE.multiply(
  72.                             alphabetSize.pow(proposalLength - prefixDepth)
  73.                     )
  74.             );
  75.         }
  76.         return result;
  77.     }
  78.    
  79.     /***************************************************************************
  80.      * Uzel stromu
  81.      */
  82.     public static class Node
  83.     {
  84.         public final int depth; //hloubka uzlu
  85.         public final Map<Character,Node> next = new HashMap<>(); //pouzly
  86.         public final Set<Integer> acceptingMembers = new HashSet<>(); //id memberů kteří příjmají podstrom proposalů
  87.         public final Set<Integer> consideringMembers = new HashSet<>(); //id memberů kteří zvažují podstrom proposalů
  88.        
  89.         public Node() {
  90.             this.depth = 0;
  91.         }
  92.        
  93.         protected Node(int depth) {
  94.             this.depth = depth;
  95.         }
  96.        
  97.         /***********************************************************************
  98.          * EXPAND - recurision
  99.          * @param criteria
  100.          * @param memberId
  101.          */
  102.         /*public void expandRecursion(String criteria, int memberId) {
  103.             expandRecursionIteration(criteria.toCharArray(), 0, memberId);
  104.         }
  105.        
  106.         public void expandRecursionIteration(char[] criteriaPart, int from, int memberId) {
  107.             this.consideringMembers.add(memberId);
  108.             if (criteriaPart.length == from) {
  109.                 this.acceptingMembers.add(memberId);
  110.                 return;
  111.             }
  112.             Node nextSelected = next.get(criteriaPart[from]);
  113.             if (nextSelected == null) {
  114.                 nextSelected = new Node(from + 1);
  115.                 next.put(criteriaPart[from], nextSelected);
  116.             }
  117.             nextSelected.expandRecursionIteration(criteriaPart, from + 1, memberId);
  118.         }*/
  119.        
  120.         /***********************************************************************
  121.          * EXPAND - stack
  122.          * stack pro obejití StackOverflow
  123.          * @param criteria
  124.          * @param memberId
  125.          */
  126.         public void expandStack(String criteria, int memberId) {
  127.             Stack<ExpandIterationTuple> stack = new Stack<>();
  128.             char[] criteriaArr = criteria.toCharArray();
  129.             stack.add(new ExpandIterationTuple(this, 0));
  130.             while (!stack.empty()) {
  131.                 ExpandIterationTuple val = stack.pop();
  132.                 val.node.expandStackIteration(stack, criteriaArr, val.from, memberId);
  133.             }
  134.         }
  135.        
  136.         public void expandStackIteration(Stack<ExpandIterationTuple> stack, char[] criteriaPart, int from, int memberId) {
  137.             this.consideringMembers.add(memberId);
  138.             if (criteriaPart.length == from) {
  139.                 this.acceptingMembers.add(memberId);
  140.                 return;
  141.             }
  142.             Node nextSelected = next.get(criteriaPart[from]);
  143.             if (nextSelected == null) {
  144.                 nextSelected = new Node(from + 1);
  145.                 next.put(criteriaPart[from], nextSelected);
  146.             }
  147.             stack.add(new ExpandIterationTuple(nextSelected, from + 1));
  148.         }
  149.        
  150.         /***********************************************************************
  151.          * COMPLY - recursion
  152.          * @param acceptThreshold
  153.          * @return
  154.          */
  155.         /*public Collection<Integer> complyRecursion(int acceptThreshold) {
  156.             return Node.this.complyRecursionIteration(0, acceptThreshold);
  157.         }
  158.        
  159.         protected Collection<Integer> complyRecursionIteration(int acceptingMembersSoFar, int acceptThreshold) {
  160.             Collection<Integer> result = new LinkedList<>();
  161.             if (acceptingMembers.size() + acceptingMembersSoFar >= acceptThreshold) {
  162.                 result.add(depth);
  163.             }
  164.             else if (consideringMembers.size() + acceptingMembersSoFar >= acceptThreshold) {
  165.                 for (Node nextOne : next.values()) {
  166.                     result.addAll(nextOne.complyRecursionIteration(acceptingMembers.size() + acceptingMembersSoFar, acceptThreshold));
  167.                 }
  168.             }
  169.             return result;
  170.         }*/
  171.        
  172.         /***********************************************************************
  173.          * COMPLY - stack
  174.          * stack pro obejití StackOverflow
  175.          * @param acceptThreshold
  176.          * @return
  177.          */
  178.         public Collection<Integer> complyStack(int acceptThreshold) {
  179.             Collection<Integer> result = new LinkedList<>();
  180.             Stack<ComplyIterationTuple> stack = new Stack<>();
  181.             stack.push(new ComplyIterationTuple(this, 0));
  182.             while (!stack.empty()) {
  183.                 ComplyIterationTuple val = stack.pop();
  184.                 val.node.complyStackIteration(result, stack, val.results, acceptThreshold);
  185.             }
  186.             return result;
  187.         }
  188.        
  189.         protected void complyStackIteration(Collection<Integer> result, Stack<ComplyIterationTuple> stack, int acceptingMembersSoFar, int acceptThreshold) {
  190.             if (acceptingMembers.size() + acceptingMembersSoFar >= acceptThreshold) {
  191.                 result.add(depth);
  192.             }
  193.             else if (consideringMembers.size() + acceptingMembersSoFar >= acceptThreshold) {
  194.                 for (Node nextOne : next.values()) {
  195.                     stack.add(new ComplyIterationTuple(nextOne, acceptingMembers.size() + acceptingMembersSoFar));
  196.                 }
  197.             }
  198.         }
  199.     }
  200.    
  201.     /***************************************************************************
  202.      * Helper class - Tuple
  203.      */
  204.     public static class ComplyIterationTuple
  205.     {
  206.         public final Node node;
  207.         public final int results;
  208.        
  209.         public ComplyIterationTuple(Node node, int results) {
  210.             this.node = node;
  211.             this.results = results;
  212.         }
  213.     }
  214.    
  215.     /***************************************************************************
  216.      * Helper class - Tuple
  217.      */
  218.     public static class ExpandIterationTuple
  219.     {
  220.         public final Node node;
  221.         public final int from;
  222.        
  223.         public ExpandIterationTuple(Node node, int from) {
  224.             this.node = node;
  225.             this.from = from;
  226.         }
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement