Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- vector<pair<int,int> > getNOP(string& s1, string& s2){
- vector< vector<int> >dp(105,vector<int>(105,0));
- vector< vector<pair<int ,int> > > prev(105,vector<pair<int ,int> >(105));
- for(int i=1;i<=s1.size();++i){
- for(int j=1;j<=s2.size();++j){
- if(s1[i-1] == s2[j-1]){
- dp[i][j] = dp[i-1][j-1]+1;
- prev[i][j] = {i-1,j-1};
- }else if(dp[i-1][j] > dp[i][j-1]){
- dp[i][j] = dp[i-1][j];
- prev[i][j] = {i-1,j};
- }else{
- dp[i][j] = dp[i][j-1];
- prev[i][j] = {i,j-1};
- }
- }
- }
- int indexI = s1.size();
- int indexJ = s2.size();
- cout<<dp[indexI][indexJ]<<endl;
- vector<pair<int,int> >ans;
- while(indexI > 0 || indexJ > 0){
- if(prev[indexI][indexJ].first == indexI-1 && prev[indexI][indexJ].second == indexJ-1 ) {
- ans.push_back({indexI,indexJ});
- }
- int newI = prev[indexI][indexJ].first;
- int newJ = prev[indexI][indexJ].second;
- indexI = newI;
- indexJ =newJ;
- }
- return ans;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement