P7377 [COCI 2018/2019 #5] Parametriziran 题解
题目传送门
观前提示:本文使用 DeepSeek 进行润色,保证人类贡献大于AI
拿到这道题,第一反应当然还是先写暴力。毕竟本题有 3s 的时限,写个朴素做法也能轻松拿到 60 pts。
暴力思路很直接:枚举任意两行,逐位比较;如果某一位上两个单词都不是 “?”,且字母不同,那么这两个单词就不相似。代码如下(测试代码请忽略): ::::info[]
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e4+5;
int n, m;
int ans = 0;
signed main()
{
// freopen("word.in","r",stdin);
// freopen("word.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
char a;
string s;
int ask_[N][6];
for(int i = 1; i <= n; i++)
{
for(int j = 1;j <= m; j++)
{
cin >> a;
if(a == '?') ask_[i][j] = 0;
else
{
ask_[i][j] = (a - 'a' + 1);
}
}
}
// for(int i = 1;i <= n; i++)
// {
// for(int j = 1;j <= m; j++)
// {
// cout << a[i][j];
// }
// cout << '\n';
// }
for(int k = 1;k <= n - 1; k++)
{
for(int i = k;i <= n; i++)
{
for(int j = 1;j <= m; j++)
{
if(ask_[k][j] != ask_[i][j])
if(ask_[k][j] != 0 && ask_[i][j] != 0)
{
// cout << a[i][j] << ' '<< i <<' '<<k<<' '<< j<<' ';
goto next;
}
}
ans++;
next:;
}
}
cout << ans - n + 1<< '\n';
return 0;
}/*
5 2
??
b?
c?
?g
cg
4 6
ab??c?
??kll?
a?k??c
?bcd??
3 3
??b
c??
c?c
暴力牛逼
但是你看我T不T飞你就完了
*/
::::
不过这样显然是会 T 的,所以接下来考虑优化。
观察暴力过程可以发现:如果两个单词的“固定位置”完全没有重合,那么它们一定相似,根本不需要逐位比较。只有在两个单词都存在非
因此,我们可以根据每个单词中非
其中
对于
特别地,当
这样,我们就不再需要枚举所有单词对,而是利用 依旧请忽略测试代码]
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 5e4+5;
int n, m;
int ans = 0;
signed main()
{
// freopen("word.in","r",stdin);
// freopen("word.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
char a;
string s;
int ask_[N][7];
int full = 1 << m;
vector<int> group[64]; // 存 mask=msk 的单词下标(从 1 开始)
for(int i = 1; i <= n; i++)
{
int msk = 0; // 这个单词的"固定位置"掩码
for(int j = 1;j <= m; j++)
{
cin >> a;
if(a == '?') ask_[i][j] = 0;
else
{
ask_[i][j] = (a - 'a' + 1);
msk |= (1 << (j - 1)); // 第 j 位(0-based)置 1
}
}
group[msk].push_back(i);
}
// for(int i = 1;i <= n; i++)
// {
// for(int j = 1;j <= m; j++)
// {
// cout << ask_[i][j];
// }
// cout << '\n';
// }
for(int A = 0; A < full; A++)
{
if(group[A].empty()) continue;
for(int B = A; B < full; B++)
{
if(group[B].empty()) continue;
int D = A & B; // 共同固定位置:A、B 都为 1 的位
if(A == B)
{
// 同组内部:按 D 上的编码分组,每组贡献 C(c,2)
unordered_map<int,int> cnt;
int szA = (int)group[A].size();
for(int t = 0; t < szA; t++)
{
int idx = group[A][t];
int code = 0;
for(int j = 1; j <= m; j++)
if(D & (1 << (j - 1)))
code = code * 27 + ask_[idx][j];
cnt[code]++;
}
unordered_map<int,int>::iterator it;
for(it = cnt.begin(); it != cnt.end(); ++it)
{
int c = it->second;
ans += c * (c - 1) / 2; // 组内两两配对
}
}
else
{
// 跨组:A 侧建表,B 侧查表
unordered_map<int,int> cnt;
int szA = (int)group[A].size();
for(int t = 0; t < szA; t++)
{
int idx = group[A][t];
int code = 0;
for(int j = 1; j <= m; j++)
if(D & (1 << (j - 1)))
code = code * 27 + ask_[idx][j];
cnt[code]++;
}
int szB = (int)group[B].size();
for(int t = 0; t < szB; t++)
{
int idx = group[B][t];
int code = 0;
for(int j = 1; j <= m; j++)
if(D & (1 << (j - 1)))
code = code * 27 + ask_[idx][j];
unordered_map<int,int>::iterator it = cnt.find(code);
if(it != cnt.end())
ans += it->second; // 该 B 词和多少个 A 词相似
}
}
}
}
cout << ans << '\n';
return 0;
}/*
5 2
??
b?
c?
?g
cg
4 6
ab??c?
??kll?
a?k??c
?bcd??
暴力牛逼
现在不 T 了:N^2 -> N * 2^M
*/
::::
最后,不求赞的题解不是好题解喵