zoukankan      html  css  js  c++  java
  • 美团2017年CodeM大赛-初赛A轮 C合并回文子串

    区间dp一直写的是递归版本的, 竟然超时了, 学了一下非递归的写法.

    #include <iostream>
    #include <sstream>
    #include <algorithm>
    #include <cstdio>
    #include <math.h>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <string.h>
    #include <bitset>
    #define REP(i,a,n) for(int i=a;i<=n;++i)
    #define PER(i,a,n) for(int i=n;i>=a;--i)
    #define hr putchar(10)
    #define pb push_back
    #define lc (o<<1)
    #define rc (lc|1)
    #define mid ((l+r)>>1)
    #define ls lc,l,mid
    #define rs rc,mid+1,r
    #define x first
    #define y second
    #define io std::ios::sync_with_stdio(false)
    #define endl '
    '
    #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pii;
    const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
    ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
    ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
    ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
    inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
    //head
    
    
    
    const int N = 55;
    char a[N], b[N];
    bool dp[N][N][N][N];
    
    int main() {
    	int t;
    	scanf("%d", &t);
    	while (t--) {
    		scanf("%s%s", a+1, b+1);
    		int n = strlen(a+1), m = strlen(b+1), ret = 0;
    		REP(d1,0,n)REP(d2,0,m) {
    			for (int l1=1,r1=d1; r1<=n; ++l1,++r1) {
    				for (int l2=1,r2=d2; r2<=m; ++l2,++r2) {
    					bool &ans = dp[l1][r1][l2][r2] = 0;
    					if (d1+d2<=1) ans = 1;
    					else {
    						if (d1>1&&a[l1]==a[r1]) ans|=dp[l1+1][r1-1][l2][r2];
    						if (d2>1&&b[l2]==b[r2]) ans|=dp[l1][r1][l2+1][r2-1];
    						if (d1&&d2) { 
    							if (a[l1]==b[r2]) ans|=dp[l1+1][r1][l2][r2-1];
    							if (b[l2]==a[r1]) ans|=dp[l1][r1-1][l2+1][r2];
    						}
    					}
    					if (ans) ret = max(ret, d1+d2);
    				}
    			}
    		}
    		printf("%d
    ", ret);
    	}
    }
    
  • 相关阅读:
    hdu 2089 不要62(数位dp)
    hdu 3555 Bomb(数位dp)
    hdu 4544 湫湫系列故事——消灭兔子(优先队列)
    STL Algorithms 之 unique
    hdu 1075 What Are You Talking About(map)
    hdu 4268 Alice and Bob(贪心+multiset)
    hdu 4302 Holedox Eating(优先队列/线段树)
    9-16Jenkins-4节点
    9-16Jenkins-3可用的环境变量、参数化构建和依赖
    9-16Jenkins-2定时任务
  • 原文地址:https://www.cnblogs.com/uid001/p/10925174.html
Copyright © 2011-2022 走看看