Saturday, January 5, 2019

[UVa] 11855 - Buzzwords

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : 11855 - Buzzwords
Source            : UVA Online Judge
Category          : String
Algorithm         : Hashing
Verdict           : Accepted
#include "bits/stdc++.h"
 
using namespace std;
 
#define FI              freopen("in.txt", "r", stdin)
#define FO              freopen("out.txt", "w", stdout)
#define FAST            ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
 
#define FOR(i, n)       for (int i = 1; i <= n; i++)
#define For(i, n)       for (int i = 0; i < n; i++)
#define ROF(i, n)       for (int i = n; i >= 1; i--)
#define Rof(i, n)       for (int i = n-1; i >= 0; i--)
#define FORI(i, n)      for (auto i : n)
 
#define ll              long long
#define ull             unsigned long long
#define vi              vector <int>
#define vl              vector <ll>
#define pii             pair <int, int>
#define pll             pair <ll, ll>
#define mk              make_pair
#define ff              first
#define ss              second
#define eb              emplace_back
#define em              emplace
#define pb              push_back
#define ppb             pop_back
#define All(a)          a.begin(), a.end()
#define memo(a, b)      memset(a, b, sizeof a)
#define Sort(a)         sort(All(a))
#define ED(a)           Sort(a), a.erase(unique(All(a)), a.end())
#define rev(a)          reverse(All(a))
#define sz(a)           (int)a.size()
#define max3(a, b, c)   max(a, max(b, c))
#define min3(a, b, c)   min(a, min(b, c))
#define maxAll(a)       *max_element(All(a))
#define minAll(a)       *min_element(All(a))
#define allUpper(a)     transform(All(a), a.begin(), :: toupper)
#define allLower(a)     transform(All(a), a.begin(), :: tolower)
#define endl            '\n'
#define nl              puts("")
#define ub              upper_bound
#define lb              lower_bound
#define Exp             exp(1.0)
#define PIE             2*acos(0.0)
#define Sin(a)          sin(((a)*PIE)/180.0)
#define EPS             1e-9
 
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
using namespace __gnu_pbds;
 
template <typename T> using orderset = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#include "ext/rope"
using namespace __gnu_cxx;
 
// rope <int> Rope;
 
// int dr[] = {1, -1, 0, 0}; // 4 Direction
// int dc[] = {0, 0, 1, -1};
// int dr[] = {0, 0, 1, -1, 1, 1, -1, -1}; // 8 Direction
// int dc[] = {1, -1, 0, 0, 1, -1, 1, -1};
// int dr[] = {-1, 1, -2, -2, -1, 1, 2, 2}; // knight Moves
// int dc[] = {-2, -2, -1, 1, 2, 2, 1, -1};
 
#define trace1(x)                           cerr << #x << ": " << x << endl;
#define trace2(x, y)                        cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)                     cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)                  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)               cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f)            cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
 
inline int setbit(int mask, int pos)        { return mask |= (1 << pos); }
inline int resetbit(int mask, int pos)      { return mask &= ~(1 << pos); }
inline int togglebit(int mask, int pos)     { return mask ^= (1 << pos); }
inline bool checkbit(int mask, int pos)     { return (bool)(mask & (1 << pos)); }
 
#define popcount(mask)                       __builtin_popcount(mask) // count set bit
#define popcountLL(mask)                     __builtin_popcountll(mask) // for long long
 
inline int read()                           { int a; scanf("%d", &a); return a; }
inline ll readLL()                          { ll a; scanf("%lld", &a); return a; }
inline double readDD()                      { double a; scanf("%lf", &a); return a; }
 
template <typename T> string toString(T num) { stringstream ss; ss << num; return ss.str(); }
int toInt(string s)                          { int num; istringstream iss(s); iss >> num; return num;  }
ll toLLong(string s)                         { ll num; istringstream iss(s); iss >> num; return num; }
 
#define inf             1e17
//#define mod             1000000007
 
static const int maxn = 1e3 + 5;
static const int logn = 18;
 
int gen_base(const int before, const int after)
{
    auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
    mt19937 mt_rand(seed);
    int base = uniform_int_distribution <int> (before+1, after) (mt_rand);
    return base % 2 == 0 ? base - 1 : base;
}
 
struct PolyHash
{
    //-------- Static variables ---------//
    static const int mod = (int)1e9+123; // prime mod of polynomial hashing
    static vector <int> pow1;            // powers of base modulo mod
    static vector <ull> pow2;            // powers of base modulo 2^64
    static int base;                     // base (point of hashing)
    //-------- Static Functions --------//
    static inline int diff(int a, int b)
    {
        // Difference between "a" and "b" modulo mod (0 <= a < mod, 0 <= b < mod)
        return (a -= b) < 0 ? a + mod : a;
    }
    //-------- Variables of class ------//
    vector <int> pref1; // Hash on prefix modulo mod
    vector <ull> pref2; // Hash on prefix modulo 2^64
    // Construct from string:
    PolyHash(const string &s) : pref1(s.size() + 1u, 0), pref2(s.size() + 1u, 0)
    {
        assert(base < mod);
        const int n = s.size(); // Firstly calculated needed power of base:
        while((int)pow1.size() <= n)
        {
            pow1.emplace_back(1LL * pow1.back() * base % mod);
            pow2.emplace_back(pow2.back() * base);
        }
        for (int i = 0; i < n; i++) // Fill arrays with polynomial hashes on prefix
        {
            assert(base > s[i]);
            pref1[i+1] = (pref1[i] + 1LL * s[i] * pow1[i]) % mod;
            pref2[i+1] = pref2[i] + s[i] * pow2[i];
        }
    }
    // Polynomial hash of subsequence [pos, pos+len)
    // If mxPow != 0, value automatically multiply on base in needed power.
    // Finally base ^ mxPow
    inline pair<int, ull> operator () (const int pos, const int len, const int mxPow = 0) const
    {
        int hash1 = pref1[pos+len] - pref1[pos];
        ull hash2 = pref2[pos+len] - pref2[pos];
        if (hash1 < 0) hash1 += mod;
        if (mxPow != 0)
        {
            hash1 = 1LL * hash1 * pow1[mxPow-(pos+len-1)] % mod;
            hash2 = hash2 * pow2[mxPow-(pos+len-1)];
        }
        return make_pair(hash1, hash2);
    }
};
 
#define piu        pair <int, ull>
 
// Initialize static variables of PolyHash class:
int PolyHash::base((int)1e9+7);
vector <int> PolyHash::pow1{1};
vector <ull> PolyHash::pow2{1};
 
int main()
{
      //FI;
      FAST;
 
      PolyHash::base = gen_base(256, PolyHash::mod);
 
      string s;
      while (getline(cin, s))
      {
            string str = "", st;
            istringstream iss(s);
            while (iss >> st)
            {
                  str += st;
                  iss.clear();
            }
            int len = sz(str);
            int mxPow = len;
 
            PolyHash hsh(str);
 
            for (int i = 1; i <= len; i++)
            {
                  map <piu, int> Map;
                  int maxi = 0;
                  for (int j = 0; j + i <= len; j++)
                  {
                        piu h = hsh(j, i, mxPow);
                        int cnt = ++Map[ h ];
                        if (cnt > maxi) maxi = cnt;
                  }
                  if (maxi <= 1)
                  {
                        cout << endl;
                        break;
                  }
                  else
                  {
                        cout << maxi << endl;
                  }
            }
      }
}
 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.