結果

問題 No.866 レベルKの正方形
ユーザー nobukichinobukichi
提出日時 2019-08-28 04:33:20
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 2,511 bytes
コンパイル時間 537 ms
コンパイル使用メモリ 62,176 KB
実行使用メモリ 43,928 KB
最終ジャッジ日時 2024-04-28 20:35:48
合計ジャッジ時間 9,985 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 TLE -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
// #include <math.h>
using namespace std;

typedef unsigned CHARSET;
const CHARSET ALLCHARS = 0x3FFF;

inline CHARSET setChar(CHARSET org, char chr) 
{
    return (org | (0x01 << (chr - 'a')));
}

//! return -1 when the number of bits set in cs < val
//! return 0 when the number of bits set in cs == val
//! return +1 when the number of bits set in cs > val
inline int check(CHARSET cs, int val) {
    if (cs == ALLCHARS) {
        return (val == 26 ? 0 : 1);
    }

    int cnt = 0;
    for (unsigned i = 0, m = 0x01; i < 26; ++i, m <<= 1) {
            if ((m & cs) != 0) {
                ++cnt;
                if (val < cnt) {
                    return 1;
                }
            }
    }

    return (val == cnt ? 0 : -1);
}



int main()
{
    int H, W, K;
    cin >> H >> W >> K;

    vector<char> c(H*W);
    for (int y = 0; y < H; ++y) {
        for (int x = 0; x < W; ++x) {
            cin >> c[W * y + x];
        }
    }

    int minLen = 1; //! min length of a side of the square with level K
    while (minLen * minLen < K) {
        ++minLen;
    }

    int maxLen = min(H, W); //! max length of a side of the square

    int ans = 0;

    vector<CHARSET> charSets(H*W); //! charSets[W * y + x] contains the CHARSET for the square with top left corner on (x, y)

    for (int y = 0; y < H - minLen + 1; ++y) {
        for (int x = 0; x < W - minLen + 1; ++x) {
            CHARSET& s = charSets[W * y + x];
            for (int oy = 0; oy < minLen; ++oy) {
                for (int ox = 0; ox < minLen; ++ox) {
                    s = setChar(s, c[W * (y + oy) + x + ox]);
                }
            }
            int ck = check(s, K);
            if (ck == 0) {
                ++ans;
            } else if (0 < ck) {
                s = ALLCHARS;
            }
        }
    }

    for (int len = minLen+1; len <= maxLen; ++len) {
        vector<CHARSET> nextCharSets(H*W);
        for (int y = 0; y < H - len + 1; ++y) {
            for (int x = 0; x < W - len + 1; ++x) {
                CHARSET& s = nextCharSets[W * y + x];
                s = charSets[W * y + x] | charSets[W * (y+1) + x] | charSets[W * y + x+1] | charSets[W * (y+1) + x+1]; 
                int ck = check(s, K);
                if (ck == 0) {
                    ++ans;
                } else if (0 < ck) {
                    s = ALLCHARS;
                }
            }
        }
        charSets.swap(nextCharSets);
    }

    cout << ans << endl;

    return 0;
}

0