zoukankan      html  css  js  c++  java
  • 51nod 1503 猪和回文(dp滚存)

    题面

    大意:在一个n*m的矩形中从(1,1)走到(n,m)而且走过的路径是一条回文串,统计方案数

    sol:我们考虑从(1,1)和(n,m)两端开始算,这样就只要保证每次经过的字符一样就可以满足回文了,因为一定有一个循环需要枚举步数,知道了步数自然只要知道了x坐标就可以算出y坐标了,于是只要枚举x1和x2了,因为当前这步一定是从上一步转移过来的,就可以滚存了

    #include<bits/stdc++.h>
    using namespace std;
    #define Mod 1000000007
    long long n, m, f[2][505][505], ans = 0;
    char Map[505][505];
    inline void Add(long long &x, long long y){x = (x + y) % Mod;}
    int main()
    {
        freopen("51nod1503.in","r",stdin);
        while(~scanf("%lld%lld", &n, &m))
        {
            ans = 0;
            for(long long i = 1; i <= n; i++)
                scanf("%s", Map[i] + 1);
            if(Map[1][1] != Map[n][m])
            {
                printf("0
    ");
                continue;
            }
            long long cur = 0;
            f[cur][1][n] = 1;
            for(long long step = 1; step <= (n + m - 2) / 2; step++)
            {
                cur ^= 1;
                for(long long i = 1; i <= n; i++)
                for(long long j = 1; j <= n; j++)
                    f[cur][i][j] = 0;
                for(long long x1 = 1; x1 <= n && x1 - 1 <= step; x1++)
                for(long long x2 = n; x2 >= 1 && n - x2 <= step; x2--)
                {
                    long long y1 = 1 + (step - (x1 - 1));
                    long long y2 = m - (step - (n - x2));
                    if (Map[x1][y1] != Map[x2][y2]) continue;
                    Add(f[cur][x1][x2], f[cur ^ 1][x1 - 1][x2 + 1]);
                    Add(f[cur][x1][x2], f[cur ^ 1][x1 - 1][x2]);
                    Add(f[cur][x1][x2], f[cur ^ 1][x1][x2 + 1]);
                    Add(f[cur][x1][x2], f[cur ^ 1][x1][x2]);
                }
            }
            for(long long i = 1; i <= n; i++)
                Add(ans, f[cur][i][i]);
            if((n + m) & 1)
            for(long long i = 1; i < n; i++)
                Add(ans, f[cur][i][i + 1]);
            printf("%lld
    ", ans);
        }
    }
    View Code
    河田は河田、赤木は赤木……。 私は誰ですか。教えてください、私は誰ですか。 そうだ、俺はあきらめない男、三井寿だ!
  • 相关阅读:
    《冒号课堂》目录 书名:冒号课堂——编程范式与OOP思想
    很好的WEB打印网页打印功能
    桌面搜索程序 Python
    面向对象保存爬虫数据 Python
    爬取微博热搜榜 李白之死 Python
    雪中悍刀行热播,来做一篇关于python的作业 爬虫与数据分析
    几个简单的例子 巩固Xpath语法 Python
    替换特殊字符 Python
    爬取酷狗榜单并可视化词云 Python
    Selenium尝试更改useragent 基于Python
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/9445534.html
Copyright © 2011-2022 走看看