COJ 1172 Generating Queue

经过 Staginner 大牛的悉心指点,终于AC了(虽然那个测试数据有点小小的问题);

dp好题(对于像我这样的初学者来说,是一道有新意的题);

# include <stdio.h>

# include <string.h>



# define MAXN 1005



int n;

char g[MAXN], s[MAXN], f[MAXN][MAXN];



int dp(int i, int j);



int main()

{

    int i;

        

    while (~scanf("%d", &n))

    {

        for (i = 1; i <= n; ++i)

            scanf("%d", &g[i]);

           for (i = 1; i <= n; ++i)

               scanf("%d", &s[i]);

          

        memset(f, -1, sizeof(f));

          

        printf(dp(1,n)==1 ? "YES\n":"NO\n");

    }

    

    return 0;

}



int dp(int i, int j)

{

    if (i == j)            return f[i][j] = (g[i] == s[1]);   /* if (i > j) return f[i][j] = 1*/

    if (f[i][j] != -1)     return f[i][j];    

    return f[i][j] = ((g[i]==s[j-i+1]) && dp(i+1,j)) || (g[j]==s[j-i+1] && dp(i,j-1));

}

 

 

你可能感兴趣的:(Queue)