Advertisement
Saleh127

Light OJ 1044 / Palindrome Partition - DP

Apr 12th, 2022
653
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. /***
  2.  created: 2022-04-12-16.19.54
  3. ***/
  4.  
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. #define ll long long
  9. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  10. #define get_lost_idiot return 0
  11. #define nl '\n'
  12.  
  13. int main()
  14. {
  15.     ios_base::sync_with_stdio(0);
  16.     cin.tie(0);
  17.     cout.tie(0);
  18.  
  19.     test
  20.     {
  21.         string a;
  22.  
  23.         cin>>a;
  24.  
  25.         ll n,m,i,j,k,l;
  26.  
  27.         n=a.size();
  28.  
  29.         bool pal[n+4][n+4];
  30.  
  31.         memset(pal,0,sizeof pal);
  32.  
  33.         for(i=0;i<n;i++) pal[i][i]=1;
  34.  
  35.         for(i=0;i<n-1;i++)
  36.         {
  37.             if(a[i]==a[i+1]) pal[i][i+1]=1;
  38.         }
  39.  
  40.         for(ll len=3;len<=n;len++)
  41.         {
  42.             for(i=0;i<n-len+1;i++)
  43.             {
  44.                 j=i+len-1;
  45.  
  46.                 if(a[i]==a[j] && pal[i+1][j-1])
  47.                 {
  48.                     pal[i][j]=1;
  49.                 }
  50.             }
  51.         }
  52.  
  53.         ll ans[n+4]={0};
  54.  
  55.         for(i=0;i<n;i++)
  56.         {
  57.             ll x=INT_MAX;
  58.  
  59.             if(pal[0][i]==1) ans[i]=0;
  60.             else
  61.             {
  62.                 for(j=0;j<i;j++)
  63.                 {
  64.                     if(pal[j+1][i]==1 && x>ans[j]+1)
  65.                     {
  66.                         x=ans[j]+1;
  67.                     }
  68.                 }
  69.                 ans[i]=x;
  70.             }
  71.         }
  72.  
  73.         cout<<"Case "<<cs<<": "<<ans[n-1]+1<<nl;
  74.  
  75.  
  76.     }
  77.  
  78.     get_lost_idiot;
  79. }
  80.  
  81.  
  82. /*
  83.  
  84. #include <bits/stdc++.h>
  85. using namespace std;
  86. #define ll long long
  87. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  88. #define get_lost_idiot return 0
  89. #define nl '\n'
  90.  
  91. ll dp[1005];
  92. string a;
  93.  
  94.  
  95. bool checkPal(ll i,ll j)
  96. {
  97.     while(i<j)
  98.     {
  99.         if(a[i]!=a[j]) return 0;
  100.  
  101.         i++;
  102.         j--;
  103.     }
  104.     return  1;
  105. }
  106.  
  107. ll solve(ll in)
  108. {
  109.     if(dp[in]!=-1) return dp[in];
  110.  
  111.     if(in>=a.size()) return 0;
  112.  
  113.     ll ans=INT_MAX;
  114.  
  115.     for(ll i=in;i<a.size();i++)
  116.     {
  117.         if(checkPal(in,i))
  118.         {
  119.             ans=min(ans,1+solve(i+1));
  120.         }
  121.     }
  122.     return dp[in]=ans;
  123. }
  124.  
  125. int main()
  126. {
  127.     ios_base::sync_with_stdio(0);
  128.     cin.tie(0);
  129.     cout.tie(0);
  130.  
  131.     test
  132.     {
  133.         cin>>a;
  134.  
  135.         memset(dp,-1,sizeof dp);
  136.  
  137.         cout<<"Case "<<cs<<": "<<solve(0)<<nl;
  138.     }
  139.  
  140.     get_lost_idiot;
  141. }
  142.  
  143. */
  144.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement