• 牛客多校第四场 F Beautiful Garden


    链接:https://www.nowcoder.com/acm/contest/142/F
    来源:牛客网

    题目描述

    There's a beautiful garden whose size is n x m in Chiaki's house. The garden can be partitioned into n x m equal-sized square chunks. There are some kinds of flowers planted in each square chunk which can be represented by using lowercase letters.
    However, Chiaki thinks the garden is not beautiful enough. Chiaki would like to build a water pool in the garden. So that the garden would look like symmetric (both horizontally and vertically). The water pool is a rectangle whose size is p x q and the center of the water pool is also the center of the garden.
    Something else important you should know is:
    • n, m, p and q are all even.
    • p is always less than n.
    • q is always less than m.
    • The borders of the water pool are parallel to the border of garden.
    Chiaki would like to know the number of different pairs of (p, q) she can choose.

    输入描述:

    There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 100) indicating the number of test cases. For each test case:
    The first line contains two integers n and m (1 ≤ n, m ≤ 2000, n and m are even) -- the size of the garden. For next n lines, each line contains m characters showing the garden. It is guaranteed that only lowercase letters will appear.

    输出描述:

    For each test case, output an integer indicating the number of choices to build the water pool.
    示例1

    输入

    3
    6 8
    acbbbbca
    dcaccacd
    cdaddadc
    cdaddadc
    dcaccacd
    acbbbbca
    6 8
    acbcbbca
    dcaccacd
    cdaddadc
    cdaddadc
    dcaccacd
    acbbbbca
    6 8
    acbbbbca
    dcadcacd
    cdaddadc
    cdaddadc
    dcaccacd
    acbbbbca

    输出

    6
    0
    3

    题意:在一个n*m的花园,我们想要这个花园变得更加完美,也就是行对称,列对称,但是花园开始可能是不对称,也可能对称,
    中间我们可以把中间的花铲走挖一个p*q池子,花园的中心就是池子的中心,来使花园看上去对称,问挖池子的方法有多少个,
    而且n,m,p,q都是偶数,p<n q<m

    思路:因为我们要使花园看上去对称,我们就去找从0开始最近的不对称的点在哪,行列分别去找,然后我们的池子必须涵盖最近的点,如果我们把
    最近的错误点盖住就可以满足了,然后再看最近的点和边界相差多少,行列间隔的距离相乘就是答案,因为我没扩展的时候有x个,我们延伸两个的时候
    ,又有x个
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 2E3 + 7;
    string s[N];
    int a[N], b[N];
    int main()
    {
        ios::sync_with_stdio(false), cin.tie(0);
        int T;
        cin >> T;
        while(T--)
        {
            int n, m;
            cin >> n >> m;
            for(int i = 0; i < n; i++)
            {
                cin >> s[i];
            }
            int num1 = 0, num2 = 0;
            for(int i = 0; i < n/2; i++)//这里我们直接用num1作间隔,然后直接匹配字符串是否相等,实际上是匹配了列上的字符是否对称
            {
                if(s[i] == s[n-i-1])
                {
                    num1++;
                }
                else
                {
                    break;
                }
            }
            for(int i = 0; i < m/2; i++)//因为行的字符不能直接用字符串相等,所以我们判断下回文
            {
                int flag = 1;
                for(int j = 0; j < n; j++)
                {
                    if(s[j][i] != s[j][m-i-1])
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                {
                    num2++;
                }
                else
                {
                    break;
                }
            }
            if(num1 == 0 || num2 == 0)
            {
                cout << 0 << endl;
            }
            else
            {
                if(num1*2 == n) num1--;//如果到了边界减一,我们不能把花园边界挖掉
                if(num2*2 == m) num2--;
                cout << num1*num2 << endl;//最终答案
            }
        }
     
    }
    主要思路:模拟+贪心
  • 相关阅读:
    30 Day Challenge Day 11 | Leetcode 55. Jump Game
    30 Day Challenge Day 11 | Leetcode 11. Container With Most Water
    30 Day Challenge Day 10 | Leetcode 701. Insert into a Binary Search Tree
    30 Day Challenge Day 10 | Leetcode 1. Two Sum
    30 Day Challenge Day 10 | Leetcode 3. Longest Substring Without Repeating Characters
    30 Day Challenge Day 9 | Leetcode 425. Word Squares
    30 Day Challenge Day 8 | Leetcode 200. Number of Islands
    30 Day Challenge Day 7 | Leetcode 235. Lowest Common Ancestor of a Binary Search Tree
    30 Day Challenge Day 7 | Hackerrank: Tree: Huffman Decoding
    30 Day Challenge Day 6 | Leetcode 92. Reverse Linked List II
  • 原文地址:https://www.cnblogs.com/Lis-/p/9383214.html
Copyright © 2020-2023  润新知