Computer Programming Contest Preparation

ToolBox - Source for: 6/628/628_passwords.cpp



/home/toolbox/public_html/solutions/6/628/628_passwords.cpp
    1 #include <cstdio>
    2 #include <iostream>
    3 #include <vector>
    4 #include <cstring>
    5 #include <cstdlib>
    6 #include <cmath>
    7 #include <algorithm>
    8 
    9 using namespace std;
   10 
   11 //#define DEBUG  //comment this line to pull out print statements
   12 #ifdef DEBUG
   13 #define TAB '\t'
   14 #define debug(a, end) cout << #a << ": " << a << end
   15 #define dbg(end) end
   16 #else
   17 #define debug(a, end)
   18 #define dbg(end)
   19 #endif
   20 
   21 typedef pair<int, int> point;
   22 typedef vector<int> vi; //?
   23 typedef vector<point> vp; //?
   24 
   25 #define UN(v) SORT(v),v.erase(unique(v.begin(),v.end()),v.end())
   26 #define SORT(c) sort((c).begin(),(c).end())
   27 #define FOR(i,a,b) for (int  i=(a); i < (b); i++)
   28 #define REP(i,n) FOR(i,0,n)
   29 #define CL(a,b) memset(a,b,sizeof(a))
   30 #define CL2d(a,b,x,y) memset(a, b, sizeof(a[0][0])*x*y)
   31 
   32 /*global variables*/
   33 vector<string> words;
   34 vector<string> rules;
   35 /*global variables*/
   36 
   37 void dump()
   38 {
   39     //dump data
   40 }
   41 
   42 bool getInput()
   43 {
   44     if (feof(stdin)) return false;
   45     //get input
   46     int num_words;
   47     scanf("%d ", &num_words);
   48     REP(i, num_words)
   49     {
   50         string x;
   51         getline(cin, x);
   52         words.push_back(x);
   53     }
   54 
   55     int num_rules;
   56     scanf("%d ", &num_rules);
   57     REP(i, num_rules)
   58     {
   59         string x;
   60         getline(cin, x);
   61         rules.push_back(x);
   62     }
   63     return true;
   64 }
   65 
   66 void process()
   67 {
   68     string word;
   69     char c[2];
   70     c[1] = 0;
   71     //process input
   72     REP(i, rules.size())
   73     {
   74         int cnt_0 = count(rules[i].begin(), rules[i].end(), '0');
   75         debug(cnt_0, TAB);
   76         //for each rule;
   77         word = rules[i];
   78         REP(j, words.size())
   79         {
   80             //for each word
   81             //go through the rule
   82             long t = pow(10, cnt_0);
   83             int p, m;
   84             for (int k = 0; k < t; ++k)
   85                 {
   86                     p = pow(10, cnt_0), m = pow(10, cnt_0-1);
   87                     debug(word, endl);
   88                     for (int l = 0; l < word.length(); ++l)
   89                         {
   90                             if (word[l] == '#')
   91                                 fputs(words[j].c_str(), stdout);
   92                             else
   93                                 {
   94                                     c[0] = '0'+(k/m%p);
   95                                     fputs(c, stdout);
   96                                     m /= 10;
   97                                     p /= 10;
   98                                 }
   99                         }
  100                     fputs("\n", stdout);
  101                 }
  102         }
  103     }
  104 }
  105 
  106 int main()
  107 {
  108     while (getInput())
  109         {
  110             printf("--\n");
  111             process();
  112 
  113             /*CLEAR GLOBAL VARIABLES!*/
  114             words.clear();
  115             rules.clear();
  116             /*CLEAR GLOBAL VARIABLES!*/
  117         }
  118 
  119     return 0;
  120 }
  121