Advertisement
Guest User

FL

a guest
Mar 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cstdio>
  6. #include <vector>
  7. #include <queue>
  8. #include <ctime>
  9. #include <algorithm>
  10. #include <map>
  11. #include <string>
  12. #include <cmath>
  13.  
  14. using namespace std;
  15. typedef long long ll;
  16.  
  17. string s;
  18. vector<ll> h, p, fibv;
  19. ll l;
  20. const ll cF = 211;
  21.  
  22. ll findLen(ll i, ll x, ll y){
  23.     return x*fibv[i - 1] + y*fibv[i - 2];
  24. }
  25.  
  26. bool is_fib(int i, int x, int y, int len){
  27.     if(len==x+y) return true;
  28.     ll mLen = findLen(i,x,y), hS2 = h[findLen(i-2,x,y)], hS1 = h[findLen(i-1,x,y)], pS = p[findLen(i-1,x,y)+1], hS = h[mLen];
  29.     return (pS*hS2==hS-hS1) && is_fib(i - 1, x, y, findLen(i-1, x, y));
  30. }
  31.  
  32. int main(){
  33.     freopen("input.txt", "r", stdin);
  34.     freopen("output.txt", "w", stdout);
  35.     getline(cin, s);
  36.     l = s.size();
  37.     fibv.push_back(0);
  38.     fibv.push_back(1);
  39.     p.push_back(0);
  40.     p.push_back(1);
  41.     h.push_back(0);
  42.     h.push_back((int)s[0]-(int)'a'+1);
  43.     for (int i = 1; i <= l; ++i){
  44.         p.push_back(p[i] * cF);
  45.     }
  46.     while (fibv.back() < l) {
  47.         ll i = fibv.size();
  48.         fibv.push_back(fibv[i - 1] + fibv[i - 2]);
  49.     }
  50.     for (int i = 2 ; i <= l; ++i){
  51.         h.push_back(h[i-1] + ((int)s[i-1]-(int)'a'+1)*p[i]);
  52.     }//end of creating hash, power of prime number, fibonacci numbers;
  53.     for (int i = fibv.size() - 1; i >= 4; --i){
  54.         ll fibX = fibv[i-1], fibY = fibv[i-2];
  55.         for (int x = 1; x <= (l/fibX)+1; ++x){
  56.             if (x*fibX>=l) continue;
  57.             if((l - x*fibX) % fibY==0)
  58.             {
  59.                 ll y = (l - x*fibX) / fibY;
  60.                         if(is_fib(i, x, y, l)){
  61.                             cout << i << endl << s.substr(l-y, y) << endl << s.substr(l-x-y, x) << endl;
  62.                             return 0;
  63.                         }
  64.             }
  65.         }
  66.     }
  67.       cout << 3 << endl
  68.         << s.substr(s.size() - 1, 1) << endl
  69.         << s.substr(0, s.size() - 1) << endl;
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement