제출 #1342085

#제출 시각아이디문제언어결과실행 시간메모리
1342085zeyruganRound words (IZhO13_rowords)C++20
0 / 100
1 ms344 KiB
//ROUND WORD
// so here we can merge b as B=B+B then any rotation of B is from B+B and then we can use LCS
//https://oj.uz/problem/view/IZhO13_rowords

#include <bits/stdc++.h>
using namespace std;
/*
//TOP DOWN APPROACH
int lcsRec(string &s1,string &s2,int m,int n,vector<vector<int>> &memo){
    //Base Case
    if(m==0||n==0){
        return 0;
    }


    //already exist in memo table
    if(memo[m][n]!=-1){
        return memo[m][n];
    }

    //match
    if(s1[m-1]==s2[n-1]){
        return memo[m][n]=1+lcsRec(s1,s2,m-1,n-1,memo);
    }

    //do not Match
    return memo[m][n]=max(lcsRec(s1,s2,m,n-1,memo),lcsRec(s1,s2,m-1,n,memo));
}



int lcs(string &s1,string &s2){
    int m=s1.length();
    int n=s2.length();

    vector<vector<int>> memo(m+1,vector<int>(n+1,-1));
    return lcsRec(s1,s2,m,n,memo);
}
*/

int solve(const string &str1,const string &str2){
    int n=str1.size();
    int m=str2.size();

    string newstr1=str1+str1;

    vector<int> dp(m+1,0),prev(m+1);
    int ans=0;
    for(int i=0;i<2*n;i++){
        prev=dp;

        for(int j=1;j<=m;j++){
            if(newstr1[i]==str2[j-1]){
                dp[j]=prev[j-1]+1;
            }
            else{
                dp[j]=max(prev[j],dp[j-1]);
            }
        }
        if(i>=n-1){
            ans=max(ans,dp[m]);
        }
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    #ifndef ONLINE_JUDGE
    freopen("rowards.in","r",stdin);
    freopen("rowards.out","w",stdout);
    #endif

    string A,B;
    cin>>A>>B;

    string newA=A+A;
    int m=newA.size();
    int n=B.size();

    int ans=0;
    ans=max(ans,solve(A,B));

    reverse(A.begin(),A.end());
    ans=max(ans,solve(A,B));
    
    cout<<ans<<endl;
    return 0;
}

컴파일 시 표준 에러 (stderr) 메시지

rowords.cpp: In function 'int main()':
rowords.cpp:70:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |     freopen("rowards.in","r",stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rowords.cpp:71:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   71 |     freopen("rowards.out","w",stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...