ABDELRHMAN_SAEED007

مسئلة قحبة تانى

Oct 23rd, 2025
1,332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.80 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ld long double
  4. #define F first
  5. #define S second
  6. #define Lnode 2*node+1
  7. #define Rnode 2*node+2
  8. #define MID (l+r>>1)
  9. #define el '\n'
  10. #define coutf(x) for(auto v:(x)) cout<<v<<' '; cout<<el
  11. #define coutp(x) for(auto v:(x)) cout<<v.F<<' '<<v.S<<el
  12. #define cinl(x) for(auto &v:(x)) cin>>v;
  13. #define all(x)  x.begin(),x.end()
  14. #define ll long long
  15. #define sz(x)  (int)x.size()
  16. #define pi pair<ll,ll>
  17. #define pii pair<ll,pair<ll,ll>>
  18. #define vi vector<ll>
  19. using ull = unsigned long long;
  20. vector<int>lcp (const string& s)
  21. {
  22.     int n=s.size();
  23.     vector<int>phi(n,0);
  24.     for (int i=1,k=0;i<n;i++)
  25.     {
  26.         k=phi[i-1];
  27.         while (k>0&&s[i]!=s[k])k=phi[k-1];
  28.         if (s[i]==s[k])k++;
  29.         phi[i]=k;
  30.     }
  31.     return phi;
  32.  
  33. }
  34.  
  35. pi mancher(const string&s)
  36. {
  37.     int c=0,r=0;
  38.     string t="#";
  39.     for (auto it:s)t+=it,t+="#";
  40.     // for (auto it:t)cout<<it<<" ";
  41.     int n=t.size();
  42.     vector<int>p(n,0);
  43.     for (int i=0;i<n;i++){
  44.         if (i<r)
  45.         {
  46.             int miror = 2*c-i;
  47.             p[i]=min(r-i,p[miror]);
  48.         }
  49.         int left=i-p[i]-1;
  50.         int right=i+p[i]+1;
  51.         while (left>=0&&right<n&&t[left]==t[right])right++,left--,p[i]++;
  52.         if (i+p[i]>r)r=i+p[i],c=i;
  53.     }
  54.     int mxlen=0,mx=0;
  55.     for (int i=1;i<n-1;i++)
  56.     {
  57.  
  58.         if (p[i]>mx)
  59.         {
  60.             if ((i-p[i])/2<=0||((i-p[i])/2)+p[i]>=s.size())
  61.             {
  62.  
  63.             mx=p[i];
  64.             mxlen=i;
  65.             }
  66.         }
  67.     }
  68.     int start=(mxlen-mx)/2;
  69.     return {start,mx};
  70. }
  71. void solve()
  72. {
  73. string s;
  74.     cin>>s;
  75.     string  rev1=s;
  76.     string ss=s;
  77.     string rev=s;
  78.     reverse(all(rev));
  79.     rev+="#"+s;
  80.     reverse(all(rev1));
  81.     s+="#"+rev1;
  82.  
  83.     auto pref=lcp(rev);
  84.     int ps=0;
  85.     for (int i=rev.size()/2+1;i<rev.size()&&pref[i];i++)ps=max(ps,pref[i]);
  86.     int mxs=pref[rev.size()-1];
  87.     auto pref2=lcp(s);
  88.     int mxp=pref2[s.size()-1];
  89.  
  90.  
  91.     string ns=ss.substr(ps,ss.size()-(2*ps));
  92.     auto pal=mancher(ns);
  93.     string ans="";
  94.  
  95.     ans+=ss.substr(0,ps);
  96.     ans+=ns.substr(pal.first,pal.second);
  97.     if (2*ps<=ss.size())
  98.     ans+=ss.substr(ss.size()-ps,ps);
  99.  
  100.  
  101.     string prefix=s.substr(0,mxp);
  102.     string suffix=s.substr(ss.size()-mxs,mxs);
  103.  
  104.     if (ans.size()>prefix.size()){
  105.  
  106.         if (ans.size()>suffix.size())cout<<ans<<"\n";
  107.         else cout<<suffix<<"\n";
  108.     }
  109.     else if (prefix.size()>suffix.size())cout<<prefix<<"\n";
  110.     else
  111.         cout<<suffix<<"\n";
  112.  
  113.  
  114. }
  115.  
  116. int32_t main() {
  117.     #ifndef ONLINE_JUDGE
  118.         freopen("in.txt", "r", stdin);
  119.         freopen("out.txt", "w", stdout);
  120.     #endif
  121.  
  122.     ios_base::sync_with_stdio(false);
  123.     cin.tie(NULL);
  124.  
  125.     int tc=1;
  126.    cin>>tc;
  127.     for (int i=1;i<=tc;i++){solve();}
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment