一、比赛基本情况
比赛日期: 8.15
比赛时长: 120
比赛得分: 156
比赛排名:1
二、比赛整体回顾
做题顺序:3-2-1
时间安排:
T1:30min
T2:45min
T3:45min
比赛状态:感觉还刑
整体感受:感觉还刑
三、题目复盘
P6824 「EZEC-4」可乐
初始思路:
由于我不会用字典树写这道题,所以只打了个暴力。
考场代码:
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2000020,B=13131;
int a[N];
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
int maxn=0;
for(int x=0;x<=10000;x++)
{
int cnt=0;
for(int i=1;i<=n;i++)
{
if((a[i]^x)>k)cnt++;
}
maxn=max(maxn,cnt);
if(cnt==n)
{
cout<<n;
return 0;
}
}
cout<<maxn;
return 0;
}正确思路:
对于从高位往低位的第j位:
①:k的第j位为一,a[i]的第j位为1:
x取1:异或得0
合法的x:x+2^j~x+2^(j+1)-1;
x取0:异或得1
继续往低位算
②:k的第j位为一,a[i]的第j位为1:
x取1:异或得1
x+2^j,继续往低位算
x取0:异或得0
合法的x:x~x+2^j-1;
③:k的第j位为0,a[i]的第j位为1:
x取1:异或得0
x+2^j,继续往低位算
x取0:异或得1
不行
④:k的第j位为0,a[i]的第j位为1:
x取1:异或得1
不行
x取0:异或得0
继续往低位算
正确代码:
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define ull unsigned long long
using namespace std;
const int N=2000020;
int sub[N],a[N];
void add(int x,int y)
{
sub[x]+=1;
sub[y+1]-=1;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,k;
cin>>n>>k;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)
{
int x=0;
for(int j=19;j>=0;j--)
{
if(k&(1<<j))
{
if(a[i]&(1<<j))
{
add(x+(1<<j),x+(1<<(j+1))-1);
}
else
{
add(x,x+(1<<j)-1);
x=x+(1<<j);
}
}
else
{
if(a[i]&(1<<j))
{
x=x+(1<<j);
}
}
}
add(x,x);
}
int maxi=0;
for(int i=1;i<=2000010;i++)
{
sub[i]+=sub[i-1];
maxi=max(maxi,sub[i]);
}
cout<<maxi;
return 0;
}P1470 [IOI 1996 / USACO2.3] 最长前缀 Longest Prefix
本题得分:84
初始思路:
dp[i]:前缀长度为i是否为符合条件的前缀。
答案:最大的dp[i]为1的i
状态转移方程:
如果子串s[i~j]在p集合中出现过,并且dp[j-1]==1,那么dp[i]为1。
边界条件:
dp[p[cnt].size()]=1;
考场代码:
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2000020,B=13131;
string p[N];
bool dp[N];
map<ull,bool>mp;
ull haxi[N],cf[N];
int gethaxi(string s)
{
int ans=s[0],last=0;
for(int i=1;i<s.size();i++)
{
last=ans;
ans=last*B+s[i];
}
return ans;
}
void init(string s)
{
for(int i=1;i<=s.size();i++)
{
haxi[i]=haxi[i-1]*B+s[i];
}
}
int f(int x,int y)
{
return haxi[y]-haxi[x-1]*cf[y-x+1];
}
signed main()
{
//freopen("t3.in","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cf[0]=1;
for(int i=1;i<=200020;i++)
{
cf[i]=cf[i-1]*B;
}
int cnt=1;
while(cin>>p[cnt])
{
if(p[cnt]==".")break;
mp[gethaxi(p[cnt])]=1;
dp[p[cnt].size()]=1;
cnt++;
}
cnt--;
string s,ss;
while(cin>>ss)
{
s+=ss;
}
int len=s.size();
s='@'+s;
init(s);
for(int i=1;i<=len;i++)
{
for(int j=i;j>=max(1ll,i-9);j--)
{
int x=j,y=i;
if(mp[f(x,y)]==1&&dp[j-1]==1)
{
dp[i]=1;
break;
}
}
}
for(int i=len;i>=1;i--)
{
if(dp[i]==1)
{
cout<<i;
return 0;
}
}
return 0;
}错因:没判断如果没有符合条件的前缀,输出0
正确代码:
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2000020,B=13131;
string p[N];
bool dp[N];
map<ull,bool>mp;
ull haxi[N],cf[N];
int gethaxi(string s)
{
int ans=s[0],last=0;
for(int i=1;i<s.size();i++)
{
last=ans;
ans=last*B+s[i];
}
return ans;
}
void init(string s)
{
for(int i=1;i<=s.size();i++)
{
haxi[i]=haxi[i-1]*B+s[i];
}
}
int f(int x,int y)
{
return haxi[y]-haxi[x-1]*cf[y-x+1];
}
signed main()
{
//freopen("t3.in","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cf[0]=1;
for(int i=1;i<=200020;i++)
{
cf[i]=cf[i-1]*B;
}
int cnt=1;
while(cin>>p[cnt])
{
if(p[cnt]==".")break;
mp[gethaxi(p[cnt])]=1;
cnt++;
}
cnt--;
string s,ss;
while(cin>>ss)
{
s+=ss;
}
int len=s.size();
s='@'+s;
init(s);
for(int i=1;i<=10;i++)
{
if(mp[f(1,i)]==1)dp[i]=1;
}
for(int i=1;i<=len;i++)
{
for(int j=i;j>=max(1ll,i-9);j--)
{
int x=j,y=i;
if(mp[f(x,y)]==1&&dp[j-1]==1)
{
dp[i]=1;
break;
}
}
}
for(int i=len;i>=0;i--)
{
if(dp[i]==1)
{
cout<<i;
return 0;
}
}
cout<<0;
return 0;
}P5546 [POI 2000 R3] 公共串
本题得分:72pts
初始思路:
把n个串的每一个子串都统计在umap里,最后看满足mp[子串]==n的最大子串长度。
考场代码:
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2000020,B=13131;
ull cf[N],haxi[N];
unordered_map<ull,int>mp;
unordered_map<ull,bool>vis;
void init(string s)
{
for(int i=1;i<=s.size();i++)
{
haxi[i]=haxi[i-1]*B+s[i];
}
}
signed main()
{
//freopen("t3.in","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cf[0]=1;
for(int i=1;i<=20020;i++)
{
cf[i]=cf[i-1]*B;
}
int n;
cin>>n;
string t;
for(int i=1;i<=n;i++)
{
string s;
cin>>s;
int len=s.size();
s='@'+s;
init(s);
t=s;
for(int x=1;x<=len;x++)
{
for(int y=x;y<=len;y++)
{
ull tmp=haxi[y]-haxi[x-1]*cf[y-x+1];
if(vis[tmp]==0)
{
vis[tmp]=1;
mp[tmp]++;
}
}
}
vis.clear();
}
int len=t.size();
init(t);
int maxn=0;
for(int x=1;x<=len;x++)
{
for(int y=x;y<=len;y++)
{
ull tmp=haxi[y]-haxi[x-1]*cf[y-x+1];
if(mp[tmp]==n)
{
maxn=max(maxn,y-x+1);
}
}
}
cout<<maxn;
return 0;
}正确思路:
因为如果有长度为k的最大公共子串,那么一定有长度为k-1的最大公共子串,符合单调性。
所以能用二分来做。
二分最大公共子串长度,把每一个长度为mid的子串统计在umap里,最后看有没有满足mp[子串]==n的子串。
正确代码:
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define endl '\n'
using namespace std;
const int N=2000020,B=13131;
ull cf[N],haxi[N];
unordered_map<ull,int>mp;
unordered_map<ull,bool>vis;
int n;
string s[N];
void init(string s)
{
for(int i=1;i<=s.size();i++)
{
haxi[i]=haxi[i-1]*B+s[i-1];
}
}
bool check(int len)
{
for(int i=1;i<=n;i++)
{
init(s[i]);
int lens=s[i].size();
for(int j=1;j<=lens-len+1;j++)
{
int x=j,y=j+len-1;
ull tmp=haxi[y]-haxi[x-1]*cf[y-x+1];
if(vis[tmp]==0)
{
vis[tmp]=1;
mp[tmp]++;
}
}
vis.clear();
}
int lens=s[n].size();
for(int j=1;j<=lens-len+1;j++)
{
int x=j,y=j+len-1;
ull tmp=haxi[y]-haxi[x-1]*cf[y-x+1];
if(mp[tmp]==n)return 1;
}
return 0;
}
signed main()
{
//freopen("t3.in","r",stdin);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cf[0]=1;
for(int i=1;i<=20020;i++)
{
cf[i]=cf[i-1]*B;
}
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s[i];
}
int lt=1-1,rt=2000+1;
while(lt+1<rt)
{
int mid=(lt+rt)/2;
if(check(mid))lt=mid;
else rt=mid;
}
cout<<lt;
return 0;
}