Advertisement
Alisator

PAL5_OOP5/10

Dec 2nd, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.85 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
  10.  * @date 30.11.2014
  11.  */
  12. public class Main
  13. {
  14.     public static void main(String[] args) throws IOException {    
  15.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  16.         StringTokenizer configuration = new StringTokenizer(br.readLine());
  17.         String alphabet = configuration.nextToken();
  18.         int membersCount = Integer.parseInt(configuration.nextToken());
  19.         int acceptThreshold = Integer.parseInt(configuration.nextToken());
  20.         int proposalLength = Integer.parseInt(configuration.nextToken());
  21.        
  22.         boolean[] criteriaLengthDistribution = new boolean[10000]; //custom length - just stretch if it doesnt suffice
  23.         Member[] committeeMembers = new Member[membersCount];
  24.         for (int i = 0; i < membersCount; i++) {
  25.             int evaluationCount = Integer.parseInt(br.readLine());
  26.             Set<String> evaluationCriteria = new HashSet<>();
  27.             for (int j = 0; j < evaluationCount; j++) {
  28.                 String line = br.readLine();
  29.                 criteriaLengthDistribution[line.length()] = true;
  30.                 evaluationCriteria.add(line);
  31.             }
  32.             committeeMembers[i] = new Member(evaluationCriteria);
  33.         }
  34.        
  35.         int minCriteriaLength = 0;
  36.         for (int i = 0; i < criteriaLengthDistribution.length; i++) {
  37.             if (criteriaLengthDistribution[i]) {
  38.                 minCriteriaLength = i;
  39.                 break;
  40.             }
  41.         }
  42.         Set<String> minCriterias = new HashSet<>();
  43.         for (Member m : committeeMembers) {
  44.             for (String c : m.evaluationList)
  45.                 minCriterias.add(c.substring(0, minCriteriaLength));
  46.         }
  47.        
  48.         Committee committee = new Committee(acceptThreshold, alphabet, criteriaLengthDistribution, committeeMembers);
  49.         BigInteger result = committee.validateAllProposalsDynamic(proposalLength, new ArrayList<String>(minCriterias));
  50.        
  51.         System.out.println(result.mod(BigInteger.valueOf(100000)));
  52.     }
  53.    
  54.     /***************************************************************************
  55.      *
  56.      */
  57.     public static class Committee
  58.     {
  59.         private final List<Member> memberList;
  60.         private final List<String> alphabetList;
  61.         private final BigInteger alphabetSize;
  62.         private final boolean[] criteriaLengthDistribution;
  63.         private final int acceptThreshold;
  64.         private final int rejectThreshold;
  65.  
  66.         public Committee(int acceptThreshold, String alphabet, boolean[] criteriaLengthDistribution, Member... members) {
  67.             this.acceptThreshold = acceptThreshold;
  68.             this.rejectThreshold = members.length - acceptThreshold + 1;
  69.             this.alphabetList = new LinkedList<>();
  70.             this.alphabetSize = BigInteger.valueOf(alphabet.length());
  71.             for (char c : alphabet.toCharArray())
  72.                 alphabetList.add(Character.toString(c));
  73.             this.memberList = Arrays.asList(members);
  74.             this.criteriaLengthDistribution = criteriaLengthDistribution;
  75.         }
  76.  
  77.         public BigInteger validateAllProposalsDynamic(int maxLen) {
  78.             List<String> startProposals = new LinkedList<>();
  79.             startProposals.add("");
  80.             return validateProposalsDynamic(0, maxLen, BigInteger.ZERO, startProposals);
  81.         }
  82.        
  83.         public BigInteger validateAllProposalsDynamic(int maxLen, List<String> startProposals) {
  84.             return validateProposalsDynamic(startProposals.get(0).length(), maxLen, BigInteger.ZERO, startProposals);
  85.         }
  86.  
  87.         private BigInteger validateProposalsDynamic(int curLen, int maxLen, BigInteger carryResult, List<String> carryProposal) {
  88.             if (carryProposal.isEmpty()) { //nothing to carry -> calculate result
  89.                 return carryResult.multiply(alphabetSize.pow(maxLen - curLen));
  90.             }
  91.             if (!criteriaLengthDistribution[curLen]) {
  92.                 List<String> consideringProposals = new LinkedList<>();
  93.                 for (String proposalBase : carryProposal)
  94.                     for (String proposalExtension : alphabetList)
  95.                         consideringProposals.add(proposalBase + proposalExtension);
  96.                 return validateProposalsDynamic(curLen + 1, maxLen, carryResult.multiply(alphabetSize), consideringProposals);
  97.             }
  98.             List<String> consideringProposals = new LinkedList<>();
  99.             BigInteger newResult = BigInteger.ZERO;
  100.             for (String proposalBase : carryProposal) {
  101.                 for (String proposalExtension : alphabetList) {
  102.                     String proposal = proposalBase + proposalExtension;
  103.                     //approved proposal
  104.                     int approvedMemberCount = 0;
  105.                     for (Member member : memberList) {
  106.                         if (member.approves(proposal))
  107.                             approvedMemberCount++;
  108.                     }
  109.                     if (approvedMemberCount >= acceptThreshold) {
  110.                         newResult = newResult.add(BigInteger.ONE);
  111.                     }
  112.                     else {
  113.                         //considered proposal
  114.                         int disagreedMemberCount = 0;
  115.                         for (Member member : memberList) {
  116.                             if (!member.considers(proposal))
  117.                                 disagreedMemberCount++;
  118.                         }
  119.                         if (disagreedMemberCount < rejectThreshold) {
  120.                             consideringProposals.add(proposal);
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.             return validateProposalsDynamic(curLen + 1, maxLen, carryResult.multiply(alphabetSize).add(newResult), consideringProposals);
  126.         }
  127.     }
  128.    
  129.     /***************************************************************************
  130.      *
  131.      */
  132.     public static class Member
  133.     {
  134.         private final Set<String> evaluationList;
  135.  
  136.         public Member(String... evaluationList) {
  137.             this.evaluationList = new HashSet<>(Arrays.asList(evaluationList));
  138.         }
  139.        
  140.         public Member(Set<String> evaluationSet) {
  141.             this.evaluationList = evaluationSet;
  142.         }
  143.        
  144.         public boolean considers(String proposal) {
  145.             for (String evalCriteria : evaluationList)
  146.                 if (evalCriteria.startsWith(proposal) || proposal.startsWith(evalCriteria))
  147.                     return true;
  148.             return false;
  149.         }
  150.        
  151.         public boolean approves(String proposal) {
  152.             for (String evalCriteria : evaluationList)
  153.                 if (proposal.startsWith(evalCriteria))
  154.                     return true;
  155.             return false;
  156.         }
  157.     }
  158.    
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement