Dev Skill Hide and Seek
//Accepted
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef map<char, bool>mpcb;
const int mod = 1000007;
const int high = 100003;
const int inf = 100000005;
char s[high];
mpcb mp;
int ans=inf;
void whole(int len)
{
int i=1 , st=1 , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i<=len)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (i - st + 1));
st=i+1;
}
}
i++;
}
}
void rev(int len)
{
int i=len , st=len , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i>=1)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (st - i + 1));
st=i-1;
}
}
i--;
}
}
void Left(int b, int mid)
{
int i=mid , st=mid , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i>=b)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (st - i + 1));
st=i-1;
}
}
i--;
}
i=b , st=b , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i<=mid)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (i - st + 1));
st=i+1;
}
}
i++;
}
}
void Right(int mid, int e)
{
int i=e , st=e , cnt=0;
mid++;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i>=mid)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (st - i + 1));
st=i-1;
}
}
i--;
}
i=mid , st=mid , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i<=e)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (i - st + 1));
st=i+1;
}
}
i++;
}
}
int main()
{
int t , n , i , len, cnt=0, st=0;
sf("%d", &t);
while(t--)
{
ans = inf;
mp['F']=mp['M']=mp['J']=mp['T']=false;
sf("%d", &n);
getchar();
for(i=1; i<=n; i++)
{
sf("%c", &s[i]);
}
whole(n); //cerr << ans << "\n";
rev(n); //cerr << ans << "\n";
Left(1, n/2); //cerr << ans << "\n";
Left(1, (n/2)+1);
Left(1, (n/2)-1);
Left(1, (n/2)+4);
Right((n/2)+1, n); //cerr << ans << "\n";
Right((n/2), n);
Right((n/2)-1, n);
Right((n/2)+4, n);
pf("%d\n", ans);
}
return 0;
}
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
using namespace std;
#define fast ios_base::sync_with_stdio(false)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef map<char, bool>mpcb;
const int mod = 1000007;
const int high = 100003;
const int inf = 100000005;
char s[high];
mpcb mp;
int ans=inf;
void whole(int len)
{
int i=1 , st=1 , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i<=len)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (i - st + 1));
st=i+1;
}
}
i++;
}
}
void rev(int len)
{
int i=len , st=len , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i>=1)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (st - i + 1));
st=i-1;
}
}
i--;
}
}
void Left(int b, int mid)
{
int i=mid , st=mid , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i>=b)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (st - i + 1));
st=i-1;
}
}
i--;
}
i=b , st=b , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i<=mid)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (i - st + 1));
st=i+1;
}
}
i++;
}
}
void Right(int mid, int e)
{
int i=e , st=e , cnt=0;
mid++;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i>=mid)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (st - i + 1));
st=i-1;
}
}
i--;
}
i=mid , st=mid , cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
while(i<=e)
{
if(!mp[s[i]])
{
cnt++;
mp[s[i]] = true;
if(cnt == 4)
{
cnt=0;
mp['F']=mp['M']=mp['J']=mp['T']=false;
ans = min(ans, (i - st + 1));
st=i+1;
}
}
i++;
}
}
int main()
{
int t , n , i , len, cnt=0, st=0;
sf("%d", &t);
while(t--)
{
ans = inf;
mp['F']=mp['M']=mp['J']=mp['T']=false;
sf("%d", &n);
getchar();
for(i=1; i<=n; i++)
{
sf("%c", &s[i]);
}
whole(n); //cerr << ans << "\n";
rev(n); //cerr << ans << "\n";
Left(1, n/2); //cerr << ans << "\n";
Left(1, (n/2)+1);
Left(1, (n/2)-1);
Left(1, (n/2)+4);
Right((n/2)+1, n); //cerr << ans << "\n";
Right((n/2), n);
Right((n/2)-1, n);
Right((n/2)+4, n);
pf("%d\n", ans);
}
return 0;
}
Comments
Post a Comment