Advertisement
_rashed

UVA 153

Jul 12th, 2022
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.83 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. const int OO = 1e9;
  6. const double EPS = 1e-9;
  7.  
  8. ll fact(string &s) {
  9.     ll ret = 1;
  10.     ll freq[26] = {};
  11.     ll cnt = 0;
  12.     for(char c : s) {
  13.         ret *= ++cnt;
  14.         ret /= ++freq[c-'a'];
  15.     }
  16.     return ret;
  17. }
  18.  
  19. int main()
  20. {
  21.     ios_base::sync_with_stdio(false);
  22.     cin.tie(NULL);
  23.     cout.tie(NULL);
  24.     while(true) {
  25.         string tgt;
  26.         cin >> tgt;
  27.         if(tgt == "#")
  28.             break;
  29.         string s = tgt;
  30.         sort(s.begin(),s.end());
  31.         ll ans = 1;
  32.         for(int i = 0; i < s.length(); i++) {
  33.             while(s[i] != tgt[i]) {
  34.                 //cout << "original s is " << s << "\n";
  35.                 string f = s.substr(i+1);
  36.                 ans += fact(f);
  37.                 string newf = "";
  38.                 bool changed = false;
  39.                 for(int j = 0; j < f.length(); j++) {
  40.                     if(f[j] > s[i]) {
  41.                         if(changed) {
  42.                             newf += f[j];
  43.                         }
  44.                         else {
  45.                             changed = true;
  46.                             newf += s[i];
  47.                             s[i] = f[j];
  48.  
  49.                         }
  50.                     }
  51.                     else {
  52.                         newf += f[j];
  53.                     }
  54.                 }
  55.                 for(int j = i+1; j < s.length(); j++) {
  56.                     s[j] = newf[j-i-1];
  57.                 }
  58.                 //cout << "s is now " << s << "\n";
  59.                 //cout << "ans is now " << ans << "\n";
  60.                 //return 0;
  61.             }
  62.         }
  63.         string sans = to_string(ans);
  64.         for(int i = 0; i < 10-sans.length(); i++) {
  65.             cout << " ";
  66.         }
  67.         cout << sans << "\n";
  68.     }
  69.  
  70.     return 0;
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement