Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define boAshraf ios_base::sync_with_stdio(false); cin.tie(NULL);
- #define ll long long
- #define sz(s) (int)(s).size()
- #define all(s) (s).begin(),(s).end()
- using namespace std;
- void File();
- void sol();
- const int N=5001;
- string s;
- int n;
- int common[N][N];
- int dp[N][N];
- int get(int i,int j){
- if(j==n)return 0;
- int &ret=common[i][j];
- if(~ret)return ret;
- ret=0;
- if(s[i]==s[j])ret=1+get(i+1,j+1);
- return ret;
- }
- int rec(int i,int last){
- if(i>n)return -1e9;
- if(i==n)return 0;
- int &ret=dp[i][last];
- if(~ret)return ret;
- ret=-1e9;
- if(last!=0) {
- ret = max(ret, rec(i + 1, last + 1));
- int c=min(last+1,get(i-last,i));
- int i1=i-last+c,i2=i+c;
- if(c>=last)ret=max(ret,1+rec(i+last+1,last+1));
- else if(s[i1]<s[i2])ret=max(ret,1+rec(i+c+1,c+1));
- }else{
- ret=rec(i+1,1)+1;
- }
- return ret;
- }
- void build(int i,int last){
- if(i==n) {
- cout<<s.substr(i-last,last)<<'\n';
- return;
- }
- int &ret=dp[i][last];
- if(last!=0) {
- if(ret==rec(i+1,last+1)){
- return build(i+1,last+1);
- }
- int c=min(last+1,get(i-last,i));
- int i1=i-last+c,i2=i+c;
- if(c>=last && ret==1+rec(i+last+1,last+1)) {
- cout<<s.substr(i-last,last)<<'\n';
- return build(i+last+1,last+1);
- }
- else if(s[i1]<s[i2] && ret==1+rec(i+c+1,c+1)){
- cout<<s.substr(i-last,last)<<'\n';
- return build(i+c+1,c+1);
- }
- assert(0);
- }else{
- return build(i+1,1);
- }
- }
- int main() {
- boAshraf
- File();
- int t = 1;
- // cin >> t;
- while (t--) {
- sol();
- }
- return 0;
- }
- void sol() {
- cin>>s;
- n=sz(s);
- memset(common,-1,sizeof common);
- memset(dp,-1,sizeof dp);
- cout<<rec(0,0)<<'\n';
- build(0,0);
- }
- void File() {
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
Advertisement
Add Comment
Please, Sign In to add comment