QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#560907#5810. Doubly-sorted Gridzhouyuhang10 14ms4240kbC++141.6kb2024-09-12 18:47:412024-09-12 18:47:42

Judging History

你现在查看的是最新测评结果

  • [2024-09-12 18:47:42]
  • 评测
  • 测评结果:10
  • 用时:14ms
  • 内存:4240kb
  • [2024-09-12 18:47:41]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int N = 15, M = 2e5;
const short P = 1e4 + 7;

void inc(short &x, short y) { if ((x += y) >= P) x -= P;}

int T, n, m;
char g[N][N];
int pos[26][N];

vector<vector<int>> S;
int idx = 0;

vector<int> a;
void dfs(int k) {
	if (k > n) {
		S.push_back(a);
		return;
	}
	for (int i = a[k - 1]; ~i; --i) a[k] = i, dfs(k + 1);
}

short f[2][M];

int main() {
	cin >> T;
	for (int t = 1; t <= T; ++t) {
		cin >> n >> m;
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= m; ++j) {
				cin >> g[i][j];
				if (g[i][j] >= 'a' && g[i][j] <= 'z') {
					pos[g[i][j] - 'a'][i] = max(pos[g[i][j] - 'a'][i], j);
				}
			}
		}
		
		a.resize(n + 1), a[0] = m;
		dfs(1);
		reverse(S.begin(), S.end());
		
		memset(f, 0, sizeof f), f[0][0] = 1;
		for (int c = 0; c < 26; ++c) {
			for (int i = 1; i <= n; ++i) {
				for (int j = 0, k = 0; j < S.size(); ++j) {
					vector<int> x = S[j];
					if (x[i - 1] >= x[i] + 1 && (g[i][x[i] + 1] == c + 'a' || g[i][x[i] + 1] == '.')) {
						vector<int> y = x; y[i]++;
						while (S[k] < y) ++k;
						inc(f[c & 1][k], f[c & 1][j]);
					}
				}
			}
			memcpy(f[(c & 1) ^ 1], f[c & 1], sizeof f[(c & 1) ^ 1]);
			
			for (int j = 0; j < S.size(); ++j) {
				vector<int> x = S[j];
				int flg = 1;
				for (int i = 1; i <= n; ++i) flg &= (pos[c][i] <= x[i]);
				if (!flg) f[(c & 1) ^ 1][j] = 0;
			}
		}
		
		cout << "Case #" << t << ": " << f[0][S.size() - 1] << endl;

		vector<vector<int>>().swap(S);
		idx = 0;
		memset(pos, 0, sizeof pos);
	}

	return 0;
}

/*
1
2 2
ad
c.
*/

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 14ms
memory: 4240kb

input:

40
4 4
....
....
....
....
4 4
abcd
bcde
cdef
defg
4 4
bbcc
ccde
cdef
deef
4 4
....
....
.r..
..e.
3 4
....
....
...g
1 3
f..
2 2
.b
bb
2 3
...
..f
4 4
....
....
..t.
..d.
3 3
.gn
...
...
4 4
...a
...b
...c
...d
4 4
...b
..bb
.bbb
bbbb
4 4
a...
.b..
..c.
...d
3 3
c..
glw
..w
1 4
....
1 3
d.v
4 2
..
...

output:

Case #1: 7285
Case #2: 1
Case #3: 1
Case #4: 0
Case #5: 718
Case #6: 231
Case #7: 2
Case #8: 686
Case #9: 0
Case #10: 3500
Case #11: 330
Case #12: 14
Case #13: 4096
Case #14: 2756
Case #15: 3737
Case #16: 19
Case #17: 8175
Case #18: 3737
Case #19: 7279
Case #20: 256
Case #21: 23
Case #22: 7569
Case ...

result:

ok 40 lines

Subtask #2:

score: 0
Time Limit Exceeded

Test #2:

score: 0
Time Limit Exceeded

input:

40
5 10
..........
..........
..........
..........
..........
10 10
....a.....
....c.....
....g.....
....j.....
....l.....
....m.....
....n.....
....q.....
....t.....
....v.....
10 10
..........
..........
..........
..........
..........
..........
..........
..........
...ok.....
..........
9 10
...

output:

Case #1: 8791
Case #2: 7220
Case #3: 0
Case #4: 2510
Case #5: 4299
Case #6: 1663
Case #7: 981
Case #8: 1430
Case #9: 8814
Case #10: 2877
Case #11: 2124
Case #12: 0
Case #13: 4311
Case #14: 4458
Case #15: 8699
Case #16: 3727
Case #17: 14
Case #18: 5478

result: