跳到正文

题解

题解:P16659 [GKS 2018 #H] Big Buttons

get_RP约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年7月11日
收录
2026年10月21日
更新
2026年11月22日
篇幅
约 3 分钟
题目
P16659

版本

  1. v1当前

传送门

思路:正难则反
已知每一位只有22种情况,BB 或 R(0R(0 或 1)1),所以总方案数为2n2^n。对于第ii个禁止前缀,有2n−l2^{n-l}(ll是禁止前缀的长度)。

所以得出代码:

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

int t,p,n;
long long ans;
string s;

int main () {
	cin >> t;
	for (int i = 1;i <= t;i++) {
		cin >> n >> p;
		ans = pow(2,n);
		while (p--) {
			cin >> s;
			ans -= pow(2,n - s[j].size());
		} cout << "Case #" << i << ": " << max(ans,0) << endl;
	} return 0;
}

但是9pts
当A是B的前缀时,如BB与BRBR,BB的情况中包含了BRBR的所以情况,所以要舍去。

AC代码:

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

long long t,p,n;
bool f[103];
long long ans;
string s[103];

bool cmp(string x,string y){
    if(x.size() != y.size()) return x.size() < y.size();
    return x < y;
}

int main () {
	cin >> t;
	for (int i = 1;i <= t;i++) {
		cin >> n >> p;
		ans = pow(2,n);
		for (int j = 1;j <= p;j++) {
			cin >> s[j];
		} sort(s + 1,s + p + 1,cmp);		
		for (int j = p;j > 0;j--) {
			string s1;
			for (int k = 0;k < s[j].size();k++) {
				s1 += s[j][k];
				for (int a = 1;a <= p;a++) {
					if (a == j) continue;
					if (s[a] == s1) {
						f[j] = 1;
					}
				}
			}
		} for (int j = 1;j <= p;j++) {
			if (!f[j]) ans -= pow(2,n - s[j].size());
		}
		cout << "Case #" << i << ": " << ans << endl;
		memset(f,0,sizeof(f));
	} return 0;
}

后记:

为什么满分是22分啊?!

原文