mr_dot_convict

11452-Dancing-the-Cheeky-Cheeky-UVa-mr.convict

Jul 26th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include      <bits/stdc++.h>
  2. #include      <ext/pb_ds/assoc_container.hpp>
  3. #include      <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6. #pragma GCC   optimize ("Ofast")
  7. #pragma GCC   optimize ("unroll-loops")
  8. #pragma GCC   target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  9.  
  10. #define IOS   ios_base::sync_with_stdio(false); cin.tie (nullptr)
  11. #define PREC  cout.precision (10); cout << fixed
  12. #define x     first
  13. #define y     second
  14. #define bg(x) " [ " << #x << " : " << (x) << " ] "
  15. #define un(x) sort(x.begin(), x.end()), \
  16.               x.erase(unique(x.begin(), x.end()), x.end())
  17. using   ll  = long long;
  18. using   ull = unsigned long long;
  19. using   ff  = long double;
  20. using   pii = pair<int,int>;
  21. using   pil = pair<int,ll>;
  22. typedef tree
  23. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  24. ordered_set;
  25.  
  26. struct chash {
  27.    int operator()(pii x) const { return x.x*31 + x.y; }
  28. };
  29. gp_hash_table <pii, int, chash> mp;
  30.  
  31. //seed_seq seq{
  32. //   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  33. //      (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  34. //      (uint64_t) __builtin_ia32_rdtsc(),
  35. //      (uint64_t) (uintptr_t) make_unique<char>().get()
  36. //};
  37. // mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  38.  
  39. #define debug(args...) { \
  40.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  41.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  42.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  43.    stringstream _ss(_s); \
  44.    istream_iterator<string> _it(_ss); err(_it, args); \
  45. }
  46. void err(istream_iterator<string> it) {
  47.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  48. }
  49. template<typename T, typename... Args>
  50. void err(istream_iterator<string> it, T a, Args... args) {
  51.    cerr << fixed << setprecision(15)
  52.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  53.    err(++it, args...);
  54. }
  55. /*****************************************************************************/
  56.  
  57. const int N = (int)1e5 + 10;
  58. int n;
  59. string s;
  60. int pi[N];
  61.  
  62. void pref_func(const string &st) {
  63.    pi[0] = 0;
  64.    for (int i = 1; i < n; ++i) {
  65.       int j_len = pi[i - 1];
  66.       int j = j_len - 1;
  67.  
  68.       while (j_len > 0 && st[j + 1] != st[i]) {
  69.          j_len = pi[j];
  70.          j = j_len - 1;
  71.       }
  72.       if (st[j + 1] == st[i])
  73.          ++j_len;
  74.       pi[i] = j_len;
  75.    }
  76. }
  77.  
  78. void solve() {
  79.    pref_func(s);
  80.    int best_i = -1, best_len = 0;
  81.    for (int i = n - 1; i >= 0; --i) {
  82.       int len = i + 1;
  83.       int k = len - pi[len - 1];
  84.       if (len % k == 0 && len / k == 2) {
  85.          best_i = i;
  86.          best_len = k;
  87.          break;
  88.       }
  89.    }
  90.    int offset = n - 2 * best_len;
  91.    for (int i = 0; i < 8; ++i) {
  92.       cout << s[(offset + i) % best_len];
  93.    }
  94.    cout << "...\n";
  95. }
  96.  
  97. signed main() {
  98.    IOS; PREC;
  99.    int tc;
  100.    cin >> tc;
  101.    while (tc--) {
  102.       cin >> s;
  103.       n = (int)s.size();
  104.       solve();
  105.    }
  106.  
  107.    return EXIT_SUCCESS;
  108. }
Add Comment
Please, Sign In to add comment