Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- char s[3123];
- char t[3123];
- int dp[3123][3123];
- string resp;
- int main() {
- scanf(" %s", s+1);
- scanf(" %s", t+1);
- //printf("%s\n", s+1);
- //printf("%s\n", t+1);
- dp[0][0] = 0;
- int max1 = strlen(s+1);
- int max2 = strlen(t+1);
- for(int i = 1; i<= max1; i++){
- for(int j = 1; j<=max2; j++){
- if(s[i] == t[j]){
- dp[i][j] = dp[i-1][j-1] +1;
- //printf("i = %d j = %d\n", i, j);
- }
- else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
- }
- }
- int aux = max1;
- int aux2 = max2;
- while(aux > 0 && aux2 > 0){
- if(s[aux] == t[aux2]){
- resp.push_back(s[aux]);
- aux--;
- aux2--;
- }
- else{
- if(dp[aux][aux2-1]>dp[aux-1][aux2] ) aux2--;
- else aux--;
- }
- }
- for(int i = resp.size()-1; i >= 0 ; i-- ) printf("%c", resp[i]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement