Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.35 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define FOR(i, a, b) for(auto i=a; i<=b; ++i)
  4. #define REP(i, a, b) for(auto i=a; i<b; ++i)
  5. #define FORI(i, a, b) for(auto i=a; i!=b+1-2*(a>b); i+=1-2*(a>b))
  6. #define REPI(i, a, b) for(auto i=a-(a>b); i!=b-(a>b); i+=1-2*(a>b))
  7.  
  8. using namespace std;
  9.  
  10.  
  11. class BearPasswordAny
  12. {
  13. public:
  14.     string findPassword(vector<int> val)
  15.     {
  16.         string result = "";
  17.         int N = val.size(), calc;
  18.         char ch = 'a';
  19.  
  20.         for(int i=N-1; i>=0; --i)
  21.         {
  22.             calc = find_occur(result, i+1);
  23.  
  24. //            cout<<val[i]<<" "<<calc<<endl;
  25.  
  26.             if(calc > val[i])
  27.                 return "";
  28.  
  29.             while(val[i] - calc)
  30.             {
  31.                 FOR(j, 0, i)
  32.                     result += ch;
  33.  
  34.                 ch = 'b' - ch + 'a';
  35.                 ++calc;
  36.             }
  37.  
  38. //            cout<<result<<endl;
  39.         }
  40.  
  41.         if(result.size() != N)
  42.             return "";
  43.  
  44.         return result;
  45.     }
  46.  
  47.  
  48.     int find_occur(string &str, int num)
  49.     {
  50.         int cnt = 1, ret = 0;
  51.  
  52.         if(!str.empty()  &&  cnt >= num)
  53.             ++ret;
  54.  
  55.         REP(i, 1, str.size())
  56.         {
  57.             if(str[i] == str[i-1])
  58.                 ++cnt;
  59.             else
  60.                 cnt = 1;
  61.  
  62.             if(cnt >= num)
  63.                 ++ret;
  64.         }
  65.  
  66.         return ret;
  67.     }
  68. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement