# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1056502 | vnm06 | Dungeons Game (IOI21_dungeons) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
int n, t, m;
string s[100005][4];
vector<string> izp;
int be, en, ans=0;
bool mtch(int id)
{
for(int i=0; i<t; i++)
{
for(int j=0; j<izp.size(); j++) if(izp[j]==s[id][i]) return 1;
}
return 0;
}
void solve(int le, int ri)
{
if(ri-le+1<=ans) return;
if(!izp.size())
{
int mid=(le+ri)/2;
be=mid, en=mid;
for(int j=0; j<t; j++)
{
izp.push_back(s[mid][j]);
while(be>le && (s[be-1][0]==s[mid][j] || s[be-1][1]==s[mid][j] || s[be-1][2]==s[mid][j] || s[be-1][3]==s[mid][j])) be--;
while(en<ri && (s[en+1][0]==s[mid][j] || s[en+1][1]==s[mid][j] || s[en+1][2]==s[mid][j] || s[en+1][3]==s[mid][j])) en++;
solve(le, ri);
izp.resize(0);
be=en=mid;
}
solve(le, mid-1);
solve(mid+1, ri);
}
else if(izp.size()==m || (be==le && ri==en))
{
ans=max(ans, en-be+1);
return;
}
else
{
int be1=be, en1=en;
for(int j=0; j<t; j++)
{
if(be!=le)
{
izp.push_back(s[be-1][j]);
while(be>le && mtch(be-1)) be--;
while(en<ri && mtch(en+1)) en++;
solve(le, ri);
izp.resize(izp.size()-1);
be=be1;
en=en1;
}
if(en!=ri)
{
izp.push_back(s[en+1][j]);
while(be>le && mtch(be-1)) be--;
while(en<ri && mtch(en+1)) en++;
solve(le, ri);
izp.resize(izp.size()-1);
be=be1;
en=en1;
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>t>>m;
for(int i=0; i<n; i++)
{
for(int j=0; j<t; j++) cin>>s[i][j];
}
solve(0, n-1);
cout<<ans<<endl;
return 0;
}