Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //substring
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- const int mod = 1000000007;
- const int mx = 1005;
- int dp[mx][mx];
- string s;
- ll solve(int b, int e) {
- if (b >= e) {
- return 1;
- }
- if (dp[b][e] != -1) {
- return dp[b][e];
- }
- if (s[b] == s[e]) {
- return dp[b][e] = solve(b + 1, e - 1);
- } else {
- return dp[b][e] = 0;
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- memset(dp, -1, sizeof(dp));
- cin >> s;
- int res = -1;
- for (int i = 0; i < s.size() ; i++) {
- for (int j = i; j < s.size(); j++) {
- if (solve(i, j)) {
- // cout << i << " " << j << endl;
- res = max(res, j - i + 1);
- }
- }
- }
- cout << res << endl;
- return 0;
- }
- //subsequence
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long int
- const int mod = 1000000007;
- const int mx = 1005;
- int dp[mx][mx];
- string s;
- pair<int,int>nxt[mx][mx];
- ll solve(int b, int e) {
- if (b == e) {
- return 1;
- }
- if (b > e) {
- return 0;
- }
- if (dp[b][e] != -1) {
- return dp[b][e];
- }
- if (s[b] == s[e]) {
- nxt[b][e]={b+1,e-1};
- return dp[b][e] = solve(b + 1, e - 1) + 2;
- } else {
- int val1 = solve(b + 1, e);
- int val2 = solve(b, e - 1);
- if(val1>val2){
- nxt[b][e]={b+1,e};
- }else{
- nxt[b][e]={b,e-1};
- }
- return dp[b][e] = max(val1, val2);
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- memset(dp, -1, sizeof(dp));
- cin >> s;
- // int res = -1;
- // for (int i = 0; i < s.size(); i++) {
- // for (int j = i; j < s.size(); j++) {
- // if (solve(i, j)) {
- // // cout << i << " " << j << endl;
- // res = max(res, j - i + 1);
- // }
- // }
- // }
- cout << solve(0, s.size() - 1) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement