Codeforces 570 E. Pig and Palindromes (DP)

Source: Internet
Author: User

Topic Links:

570 E. Pig and Palindromes

Title Description:

There is a n*m matrix, each small lattice inside has a letter. Peppa the Pig wants to (from) to (N, m). Because Peppa the pig was a perfectionist, she wanted the string of letters on the path she had passed was a palindrome, and now Peppa the pig wants to know how many ways to meet the conditions?

Problem Solving Ideas:

Since the letters on the path are composed of palindrome strings, you can start DP at the same time (n,m). Starting from (a) only to the lower and right, from (n,m) only to the top and left. Then you can dp[x1][y1][x2][y2], in fact, you can optimize the DP array to three-dimensional dp[step][x1][x2]. Because the number of steps from two points is the same, knowing the number of steps and the coordinates of a direction, you can find the coordinates of the other direction. But Jiangzi do, still will be MTL (personal experience >_<), but please our noble scroll array out of everything OK.

1#include <bits/stdc++.h>2 using namespacestd;3 4 Const intMAXN =510;5 Const intMoD =1000000007;6 intdp[2][MAXN][MAXN], N, m;7 CharMAPS[MAXN][MAXN];8 9 intMain ()Ten { One      while(SCANF ("%d%d", &n, &m)! =EOF) A     { -          for(intI=1; i<=n; i++) -scanf ("%s", maps[i]+1); the  -         if(maps[1][1] !=Maps[n][m]) -         { -printf ("0\n"); +             Continue; -         } +  AMemset (DP,0,sizeof(DP)); atdp[0][1][n] =1; -         intres = (n + M-2) /2; -  -          for(intI=1; i<=res; i++) -         { -              for(intj=1; j<=i+1; J + +) in                  for(intK=n; k>=n-i; k--) -                 { to                     intx1, x2, y1, y2; +X1 = J, y1 = I-j +2; -x2 = k, y2 = n + m-k-i; the                      *                     if(Maps[x1][y1] = =Maps[x2][y2]) $                     {Panax Notoginseng                         if(x1>x2 | | y1>y2) -                             Continue; thedp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j-1][K])%MoD; +dp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j-1][k+1])%MoD; Adp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][J][K])%MoD; thedp[i%2][j][k] = (dp[i%2][j][k] + dp[(i%2)^1][j][k+1])%MoD; +                     } -                      $                 } $memset (dp[(i%2)^1],0,sizeof(Dp[(i%2)^1])); -         } -  the         intAns =0; -         if((n+m)%2==0)Wuyi         { the              for(intI=1; i<=n; i++) -Ans = (ans + dp[res%2][i][i])%MoD; Wu         } -         Else About              for(intI=1; i<=n; i++) $             { -                 intx = Res-i +2; -                 if(x +1<=m) -Ans = (ans + dp[res%2][i][i])%MoD; A                 if(i +1<=N) +Ans = (ans + dp[res%2][i][i+1]) %MoD; the             } -printf ("%d\n", ans); $  the     } the     return 0; the}

Codeforces 570 E. Pig and Palindromes (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.