QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#130697#5066. String-dle CountPetroTarnavskyiTL 429ms106488kbC++172.8kb2023-07-24 19:57:222023-07-24 19:57:25

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-24 19:57:25]
  • 评测
  • 测评结果:TL
  • 用时:429ms
  • 内存:106488kb
  • [2023-07-24 19:57:22]
  • 提交

answer

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

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define FOR(i, a, b) for (int i = (a); i<(b); ++i)
#define RFOR(i, b, a) for (int i = (b)-1; i>=(a); --i)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define FILL(a, b) memset(a, b, sizeof(a))

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int mod = 1e9 + 7;
void upd(int& a, int b){
	a += b;
	if(a >= mod)
		a -= mod;
}


const int ALP = 26;
const int K = 19;

int L[ALP], R[ALP];
bool can[ALP][K];

int dp[2][2][K + 1][1 << K];

int ms[1 << K];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n, k;
	cin >> n >> k;
	FOR(i, 0, ALP){
		FOR(j, 0, k)
			can[i][j] = 1;
		R[i] = k;
		L[i] = 0;
		R[i] = k;
	}
	int ans = 1;
	FOR(i, 0, n){
		string q, a;
		cin >> q >> a;
		VI cnt(ALP);
		VI wasX(ALP);
		FOR(j, 0, k){
			cnt[q[j] - 'A'] += (a[j] != 'x');
			if(a[j] == '-'){
				if(wasX[q[j] - 'A'])
					ans = 0;
			}
			wasX[q[j] - 'A'] |= a[j] == 'x';
		}
		FOR(j, 0, k){
			int pos = q[j] - 'A';
			L[pos] = max(L[pos], cnt[pos]);
			
			if(a[j] == 'x'){
				can[pos][j] = 0;
				if(L[pos] > cnt[pos])
					ans = 0;
				if(R[pos] < cnt[pos])
					ans = 0;
				
				L[pos] = R[pos] = cnt[pos];
				continue;
			}
			
			if(a[j] == '-')
				can[pos][j] = 0;
			else{
				FOR(bad, 0, ALP)
					if(bad != pos)
						can[bad][j] = 0;
			}	
		}
	}
	
	if(ans == 0){
		cout << 0 << "\n";
		return 0;
	}
	dp[0][0][0][(1 << k) - 1] = 1;
	FOR(a, 0, ALP){		
		int t = a % 2;
		int nt = (a + 1) % 2;
		//clear
		FOR(bit, 0, 2)
			FOR(cnt, 0, k + 1)
				FOR(mask, 0, 1 << k)
					dp[nt][bit][cnt][mask] = 0;
		//FILL(dp[nt], 0);
		
		VI ok(1 << k);
		FOR(mask, 0, 1 << k)
			if(dp[t][0][0][mask] != 0)
				ok[mask] = 1;
		FOR(bit, 0, k)
			FOR(mask, 0, 1 << k)
				if(ok[mask] && (mask & (1 << bit)) != 0)
					ok[mask ^ (1 << bit)] = 1;
		int sz = 0;
		RFOR(mask, 1 << k, 0)
			if(ok[mask])
				ms[sz++] = mask;
		
		
		FOR(bit, 0, k){
			int bt = bit % 2;
			int nbt = bt ^ 1;
			//clear
			FOR(cnt, 0, k + 1)
				FOR(mask, 0, 1 << k)
					dp[t][nbt][cnt][mask] = 0;
			
			
			FOR(cnt, 0, k + 1){
				FOR(i, 0, sz){
					int mask = ms[i];	
					if(!ok[mask])
						continue;
						
					upd(dp[t][nbt][cnt][mask], dp[t][bt][cnt][mask]);
					
					if((mask & (1 << bit)) == 0)
						continue;
					if(can[a][bit] == 0)
						continue;
					if(cnt + 1 > R[a])
						continue;

					upd(dp[t][nbt][cnt + 1][mask - (1 << bit)], dp[t][bt][cnt][mask]);
				}
			}
		}
		FOR(mask, 0, 1 << k){
			FOR(cnt, L[a], R[a] + 1)
				upd(dp[nt][0][0][mask], dp[t][k % 2][cnt][mask]);
		}
	}
	cout << dp[ALP % 2][0][0][0] << endl;
	cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
	
	return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 42756kb

input:

2 5
CRANE
xx--x
NASAL
OOxOO

output:

21

result:

ok 1 number(s): "21"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3524kb

input:

1 5
BBBAA
xxxx-

output:

0

result:

ok 1 number(s): "0"

Test #3:

score: 0
Accepted
time: 1ms
memory: 3544kb

input:

2 5
ABCDE
-xxxx
ABCDE
xxxxx

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: 0
Accepted
time: 3ms
memory: 30396kb

input:

1 3
ABC
---

output:

2

result:

ok 1 number(s): "2"

Test #5:

score: 0
Accepted
time: 429ms
memory: 106388kb

input:

1 15
AAAAAAAAAAAAAAB
-xxxxxxxxxxxxxx

output:

918547951

result:

ok 1 number(s): "918547951"

Test #6:

score: 0
Accepted
time: 134ms
memory: 106488kb

input:

1 15
AAAAAAAAAAAAAAA
-xxxxxxxxxxxxxx

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 3ms
memory: 18236kb

input:

1 1
K
x

output:

25

result:

ok 1 number(s): "25"

Test #8:

score: -100
Time Limit Exceeded

input:

19 19
ZAZZZAZZZZZZZZZZAAZ
x-xxxxxxxxxxxxxxxxx
ZBZBZZBZZZZBZZZZBZZ
x-xxxxxxxxxxxxxxxxx
CZZCZZCZCZZCZZZCZZZ
-xxxxxxxxxxxxxxxxxx
ZDZZDZDZZZZZZZZZZZZ
x-xxxxxxxxxxxxxxxxx
ZZZZEEZEZZEEZZZZZZZ
xxxx-xxxxxxxxxxxxxx
ZZZZZFZZZZZZZZZZZZF
xxxxx-xxxxxxxxxxxxx
ZZGGZZZZZZZZGGGZZGZ
xx-xxxxxxxxxxxxxxxx
HHHHZHZZZZHHZZ...

output:


result: