题解:P7377 [COCI 2018/2019 #5] Parametriziran
Infinity_qwq
·
·
题解
这能放 noip 模拟赛 T4 的位置?!
暴力是容易想到的,时间复杂度为 O(n^2m),显然不够优秀,需要优化。
我们记 map 的键为对应的字符串,值为该字符串出现的次数。
由于 `?` 可以匹配成任意一个字符,所以暂且先不用考虑 `?`,使用二进制记录下确定的位置和通配符。然后我们枚举该二进制的所有子集,在对应的 map 中查找与之对应的出现次数。
例如字符串 `a?b?c?`,我们只需要考虑 `abc`,将其记录为 `101010`。然后枚举,假设当前枚举到的是 `100000`,我们在其对应的 map 中查找 `a?????`。
注意为了保证不重不漏,我们只计算当前字符串以前的串串。也就是说,我们需要在计算完当前的答案后插入当前字符串。
我们再次枚举,对于每个子集,我们构思出当前的键,并将对应的出现次数加一。还是以刚才的字符串为例,假设枚举到 `100000`,我们在对应的 map 中把 `a?????` 的位置的出现次数加一。
然后我们非常愉快的被卡了。
::::error[Wrong code]
```cpp line-numbers lines=80
/*
time : 2026-07-28 15:49:50
contest :
Problem :
Solution :
Code by 720872
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<climits>
#include<bitset>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define il inline
#define endl '\n'
#define y1 Y1
#define pii pair<int,int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define _1 first
#define _2 second
#define mkp make_pair
#define pb push_back
#define lowbit(x) x & (-x)
#define debug cout << "debug" << endl;
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
using namespace std;
inline ll read(){
ll k = 0, f = 1, c = gc();
for (; !isdigit(c); c = gc()) if (c == '-') f = -1;
for (; isdigit(c); c = gc()) k = k * 10 + (c ^ 48);
return k * f;
}
//inline void put(ll x){
// if(x < 0) putchar('-'), x = -x;
// if(x < 10) putchar(x + '0');
// else put(x / 10), putchar(x % 10 + '0');
//}
unordered_map<ll, int> mp[(1 << 6)];
int p[10], n, m, sure;
//sure 哪些位置不是问号。
ll ans;
char s[10];
void init(){
p[0] = 1;
for(int i = 1; i <= 6; i++)
p[i] = p[i - 1] * 2;
}
void dfs(int pos, int val){ //dfs 枚举子集,pos 记录位数,val 代表了这个的二进制数。
if(pos >= m){
int hash = 0;
for(int i = 0; i < m; i++){
if(!(sure & p[i])) continue;
if(val & p[i]) hash = hash * 31 + s[i];
else hash = hash * 31 + '?';
}
ans += mp[sure][hash];
return;
}
if(!(sure & p[pos]))
dfs(pos + 1, val);
else{
dfs(pos + 1, val | 0);
dfs(pos + 1, val | p[pos]);
}
}
signed main(){
// freopen("string.in", "r", stdin);
// freopen("string.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
init();
n = read(), m = read();
int maxx = (1 << m), hash;
for(int i = 1; i <= n; i++){
cin >> s;
sure = 0;
for(int j = 0; j < m; j++)
if(s[j] != '?') sure |= p[j];
dfs(0, 0);
for(int i = 0; i < maxx; i++){
hash = 0;
for(int j = 0; j < m; j++)
if(i & p[j]) hash = hash * 31 + s[j];
mp[i][hash]++;
}
}
cout << ans << endl;
return 0;
}
/*
*/
```
::::
具体是因为什么?注意到文中标亮的代码为:
```cpp
ans += mp[sure][hash];
```
这一行为会造成额外的声明和调用,为了优化,改为以下代码:
```cpp
auto it = mp[sure].find(hash);
if (it != mp[sure].end()) ans += it->second;
```
::::success[AC code]
```cpp line-numbers
/*
time : 2026-07-28 15:49:50
contest :
Problem :
Solution :
Code by 720872
*/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<climits>
#include<bitset>
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define il inline
#define endl '\n'
#define y1 Y1
#define pii pair<int,int>
#define pli pair<ll, int>
#define pll pair<ll, ll>
#define _1 first
#define _2 second
#define mkp make_pair
#define pb push_back
#define lowbit(x) x & (-x)
#define debug cout << "debug" << endl;
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
using namespace std;
inline ll read(){
ll k = 0, f = 1, c = gc();
for (; !isdigit(c); c = gc()) if (c == '-') f = -1;
for (; isdigit(c); c = gc()) k = k * 10 + (c ^ 48);
return k * f;
}
//inline void put(ll x){
// if(x < 0) putchar('-'), x = -x;
// if(x < 10) putchar(x + '0');
// else put(x / 10), putchar(x % 10 + '0');
//}
unordered_map<ll, int> mp[(1 << 6)];
int p[10], n, m, sure;
//sure 哪些位置不是问号。
ll ans;
char s[10];
void init(){
p[0] = 1;
for(int i = 1; i <= 6; i++)
p[i] = p[i - 1] * 2;
}
void dfs(int pos, int val){ //dfs 枚举子集,pos 记录位数,val 代表了这个的二进制数。
if(pos >= m){
int hash = 0;
for(int i = 0; i < m; i++){
if(!(sure & p[i])) continue;
if(val & p[i]) hash = hash * 31 + s[i];
else hash = hash * 31 + '?';
}
auto it = mp[sure].find(hash);
if (it != mp[sure].end()) ans += it->second;
return;
}
if(!(sure & p[pos]))
dfs(pos + 1, val);
else{
dfs(pos + 1, val | 0);
dfs(pos + 1, val | p[pos]);
}
}
signed main(){
// freopen("string.in", "r", stdin);
// freopen("string.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0); cout.tie(0);
init();
n = read(), m = read();
int maxx = (1 << m), hash;
for(int i = 1; i <= n; i++){
cin >> s;
sure = 0;
for(int j = 0; j < m; j++)
if(s[j] != '?') sure |= p[j];
dfs(0, 0);
for(int i = 0; i < maxx; i++){
hash = 0;
for(int j = 0; j < m; j++)
if(i & p[j]) hash = hash * 31 + s[j];
mp[i][hash]++;
}
}
cout << ans << endl;
return 0;
}
/*
大样例过了就是满分,但我在【】打模拟赛?!
*/
```
::::