Advertisement
Alisator

PAL5_3-12_NEoop

Nov 30th, 2014
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 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.HashMap;
  9. import java.util.HashSet;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.Set;
  13. import java.util.StringTokenizer;
  14.  
  15. public class Main_Alisa {
  16.  
  17.     static public Integer MAX_PROPOSAL = 100000;
  18.     static public String alphabet; // chars in alphabet
  19.     static public int countMembers; //count of comittee memebers
  20.     static public int countMembersAccept; // count of comittee memebers for acceptance of proposal
  21.     static public int proposalLength; //length of word
  22.     static public Set<String> UniqueProposals;//vschny unikatni prefixy
  23.     static public Set<String> AcceptedPrposals; //prijate unikatni prefixy
  24.     static public Set<String> AcceptedHighPrefixes;//jenom ty kratsi unikatni prefixy
  25.     static public HashMap<String, Integer> UniqueProposalsEval; //ohodnocene unikatni prefixy
  26.     static public Iterator it1;
  27.     static public Iterator it2;
  28.     public static Object[] high;
  29.     public static BigInteger alphabetSize;
  30.    
  31.     public static final int bufferSize = 10000000;
  32.  
  33.     public static void main(String[] args) throws IOException {
  34.         args = new String[]{"pub01.in"};
  35.  
  36.         BufferedReader br = (args.length == 0)
  37.                 ? new BufferedReader(new InputStreamReader(System.in), bufferSize)
  38.                 : new BufferedReader(new InputStreamReader(new FileInputStream(args[0])), bufferSize);
  39.         //BufferedReader br = new BufferedReader(new InputStreamReader(System.in), bufferSize);
  40.        
  41.         StringTokenizer configuration = new StringTokenizer(br.readLine());
  42.         alphabet = configuration.nextToken();
  43.         alphabetSize = BigInteger.valueOf(alphabet.length());
  44.         countMembers = Integer.parseInt(configuration.nextToken());
  45.         countMembersAccept = Integer.parseInt(configuration.nextToken());
  46.         proposalLength = Integer.parseInt(configuration.nextToken());
  47.  
  48.         UniqueProposals = new HashSet<>();
  49.         AcceptedPrposals = new HashSet<>();
  50.         UniqueProposalsEval = new HashMap<>();
  51.  
  52.         HashSet comitteeProposals; //uchovavame pouze unikatni navrhy clena
  53.         int countOfProp;
  54.         String prefixProp;
  55.  
  56.         for (int i = 0; i < countMembers; i++) {
  57.             comitteeProposals = new HashSet();
  58.             countOfProp = Integer.parseInt(br.readLine());
  59.             for (int j = 0; j < countOfProp; j++) {
  60.                 prefixProp = br.readLine();
  61.                 //kazdemu clenu pridame jeho unikatni navrhy - mozna nebude potreba
  62.                 evaluateProposal(prefixProp, comitteeProposals);
  63.             }
  64.             //ohodnoceni a kontrola prefixu
  65.             //  evaluateForOneComittee(comitteeProposals);
  66.         }
  67.         AcceptedHighPrefixes = new HashSet<>(AcceptedPrposals);
  68.        
  69.         Set<String> acceptedProposalPrefixes = filterRedundantPrefixes(AcceptedHighPrefixes);
  70.        
  71.         BigInteger result = countPossibilitiesFromPrefixes(acceptedProposalPrefixes, alphabetSize, proposalLength);
  72.        
  73.         System.out.println(result.mod(BigInteger.valueOf(MAX_PROPOSAL)));
  74.     }
  75.  
  76.     public static void evaluateProposal(String proposal, HashSet comitteePropsal) {
  77.         Integer valueOfExistingPrefix;
  78.         HashMap<String, Integer> uniqueCopy;
  79.         //kontroluje duplicitu u jednoho hlasujiciho
  80.         if (comitteePropsal.add(proposal)) {
  81.  
  82.             if (!AcceptedPrposals.contains(proposal)) {
  83.                 if (UniqueProposals.add(proposal)) {
  84.                     UniqueProposalsEval.put(proposal, 1);
  85.                     valueOfExistingPrefix = UniqueProposalsEval.get(proposal);
  86.                 } else {
  87.                     valueOfExistingPrefix = UniqueProposalsEval.get(proposal) + 1;
  88.                     UniqueProposalsEval.put(proposal, valueOfExistingPrefix);
  89.                 }
  90.                 // && next.length()<=K
  91.                 if (valueOfExistingPrefix >= countMembersAccept) {
  92.                     AcceptedPrposals.add(proposal);
  93.                     //skoro by se tu hodil return :D kdyz navrh prijat, nema smysl projizde uniqueeval, ale zase si neumazem potomky
  94.                 }
  95.                 uniqueCopy = new HashMap<>(UniqueProposalsEval);
  96.  
  97.                 //kdyz stringy zacinaji stejnym prefixem, odstranime vsechny vetsi potomky
  98.                 for (Map.Entry<String, Integer> entrySet : uniqueCopy.entrySet()) {
  99.                     String key = entrySet.getKey();
  100.                     Integer value = entrySet.getValue();
  101.                     //key zacina prefixem a nejsou shodne -> vetsi
  102.                     if (key.startsWith(proposal) && !key.equals(proposal)) {
  103.                         value = value + 1; //pricteme hlas k proposal
  104.                         UniqueProposalsEval.remove(key, value); //odstranime vetsiho potomka
  105.                         if (value >= countMembersAccept) { //pokud proposal prijat, break
  106.                             AcceptedPrposals.add(proposal);
  107.                             break;
  108.                         }
  109.                     }
  110.                 }
  111.                 uniqueCopy = new HashMap<>(UniqueProposalsEval);
  112.             }
  113.         }
  114.     }
  115.    
  116.     /***************************************************************************
  117.      * Vrátí prefixy bez těch které jsou odvoditelné z prefixů nižšího řádu
  118.      * @param acceptedPrefixes
  119.      * @return
  120.      */
  121.     private static Set<String> filterRedundantPrefixes(Set<String> acceptedPrefixes) {
  122.         Set<String> result = new HashSet<>();
  123.         for (String prefix : acceptedPrefixes) {
  124.             boolean acceptPrefix = true;
  125.             for (String prefixCheck : acceptedPrefixes) {
  126.                 if (prefixCheck.length() < prefix.length() && prefix.startsWith(prefixCheck)) {
  127.                     acceptPrefix = false;
  128.                     break;
  129.                 }
  130.             }
  131.             if (acceptPrefix) {
  132.                 result.add(prefix);
  133.             }
  134.         }
  135.         return result;
  136.     }
  137.    
  138.     /***************************************************************************
  139.      * Vrátí součet možných řešení které lze vygenerovat z prefixů v předané množině
  140.      * @param prefixes
  141.      * @param alphabetSize
  142.      * @param proposalLength
  143.      * @return
  144.      */
  145.     private static BigInteger countPossibilitiesFromPrefixes(Set<String> prefixes, BigInteger alphabetSize, int proposalLength) {
  146.         BigInteger result = BigInteger.ZERO;
  147.         for (String prefix : prefixes) {
  148.             result = result.add(
  149.                     BigInteger.ONE.multiply(
  150.                             alphabetSize.pow(proposalLength - prefix.length())
  151.                     )
  152.             );
  153.         }
  154.         return result;
  155.     }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement