結果

問題 No.866 レベルKの正方形
ユーザー nobukichi
提出日時 2019-08-28 04:33:20
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 2,511 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 62,532 KB
実行使用メモリ 45,244 KB
最終ジャッジ日時 2024-11-17 16:08:41
合計ジャッジ時間 100,798 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 8 TLE * 14
権限があれば一括ダウンロードができます

ソースコード

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