/*************************************************************************
> File Name: 1709.cpp
> Author: huhao
> Mail: 826538400@qq.com
> Created Time: 2017/5/1 星期一 17:55:41
************************************************************************/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-8
#define rt return
#define gc getchar()
#define ll long long
#define fr(i,a,b) for(int i=a,_end_=b;i<=_end_;i++)
#define fd(i,a,b) for(int i=a,_end_=b;i>=_end_;i--)
void SWAP(int a,int b)
{
a^=b^=a^=b;
}
int read()
{
int r=1,t=0;
char c=gc;
while(c<'0'||c>'9')
{
if(c=='-')
r=-1;
else
r=1;
c=gc;
}
while(c>='0'&&c<='9')
{
t=t*10+c-48;
c=gc;
}
rt r*t;
}
char readchar(int x,int y)
{
char c=gc;
while(c<x||c>y)
c=gc;
rt c;
}
int ans[2][5000010],n,a[10000010],t,x;
int main()
{
#ifndef ONLINE_JUDGE
freopen("", "r", stdin);
freopen("", "w", stdout);
#endif
n=read();
fr(i,1,n)
a[i]=a[n+i]=readchar('a','z')-'a'+1;
int o=0;
fr(i,1,n)
if(a[i]!=a[i-1])
ans[0][++o]=i;//减掉一些浪费时间的枝
fr(i,1,n)
{
int minn=inf,o=0;
for(int j=1;ans[x][j]!=0;j++)
minn=min(a[ans[x][j]+t],minn);//找最小
for(int j=1;ans[x][j]!=0;j++)
if(a[ans[x][j]+t]==minn)
ans[x^1][++o]=ans[x][j];//是最小就加入
x^=1;
ans[x][o+1]=0;
t++;
}
printf("%d\n",ans[x][1]-1);
rt 0;
}
若不加
if(a[i]!=a[i-1])
那么
#1
AC
0ms/88175kB
#2
AC
0ms/88175kB
#3
AC
0ms/88175kB
#4
AC
1ms/88175kB
#5
AC
1ms/88175kB
#6
AC
1ms/88175kB
#7
AC
0ms/88175kB
#8
AC
142ms/88175kB
#9
AC
139ms/88175kB
#10
TLE
90'
加了
#1
AC
0ms/88175kB
#2
AC
0ms/2363kB
#3
AC
0ms/88175kB
#4
AC
2ms/88175kB
#5
AC
4ms/88175kB
#6
AC
2ms/88175kB
#7
AC
0ms/88175kB
#8
AC
185ms/88175kB
#9
AC
182ms/88175kB
#10
AC
154ms/88175kB
100'
不过#8#9
想不出 时间为什么会变长