GYM:Winter Olympic Games(后缀数组)

W. Winter Olympic Games
time limit per test
5.0 s
memory limit per test
1024 MB
input
standard input
output
standard output
Figure: "Soohorang" - Not related to this problem, but included just because it's cute.

2018 RUN@KAIST Winter Curling Competition women's finals game is now ongoing. On the frozen "duck pond" of KAIST, Korean women curling team is having a fierce competition with team from country Jwepan!

There are N curling stones on the "duck pond". As the competition is really fierce, every stone is placed in a line from a mark. The leftmost stone is closest from the mark, while the rightmost stone is farthest from the mark. Stones are either from Korean team ('1'), or from Jwepan team ('0'). Those arrangement of stones can be represented with length N binary sequence.

After the end of Pyeongchang Olympics, Korean team had gone through intensive training. Now with some shoutings(?), team member "Youngmi", who carries the curling stone, can bounce away some consecutive stones and place her stone in that position. Formally, Korean team can pick any subsegment in a binary string (which can be empty), and replace it into a single digit "1".

Korean team is a master in a curling strategy, and they knew the best strategy for one turn is to make the string lexicographically maximal! For the fast decision making in this game, they want to find a fastest algorithm which can find this. Help the Korean team to win the competition!

String s = s1s2... sn of length n is lexicographically larger than string t = t1t2... tm of length m, if one of the following holds:

  • There exists some i such that, s1 = t1s2 = t2...si - 1 = ti - 1, and si > ti.
  • n > m and, s1 = t1s2 = t2...sm = tm.
Input

In the first line, N, the number of stones is given. (1 ≤ N ≤ 1, 000, 000)

In the second line, A single binary string of length N, which consists of '0' or '1' is given. This string indicates the owner of each curling stone, in the order of distance from the mark. There are no quotes or blanks given in the string.

Output

Print two integer S,  L. This means that Youngmi removed L stones after Sth character. If there is more than one correct answer, print any. (0 ≤ S,  L ≤ N)

Examples
input
Copy
8
10101101
output
Copy
1 3
input
Copy
5
11111
output
Copy
0 0

题意:给一个01串,可以替换一个任意长度的连续串为一个1,问最终的字典序最大的串?

思路:一道水题愣是弄了一个上午,注意连续串长度可以为空,第一个0肯定是作为起点,然后找到其后字典序最大的后序,将两者中间的替换成1就是最佳答案,后缀数组就行。

# include <bits/stdc++.h>
using namespace std;
const int N = 1e6+30;
int wa[N],wb[N],wsf[N],wv[N],sa[N];
int s[N],ss[N], a[N],n,id[N],kk;
char str[N];
int cmp(int *r,int a,int b,int k){return r[a]==r[b]&&r[a+k]==r[b+k];}
void getsa(int *r,int *sa,int n,int m)
{
    int i,j,p,*x=wa,*y=wb,*t;
    for(i=0; i<m; i++)  wsf[i]=0;
    for(i=0; i<n; i++)  wsf[x[i]=r[i]]++;
    for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
    for(i=n-1; i>=0; i--)  sa[--wsf[x[i]]]=i;
    j=p=1;
    for(; p<n; j*=2,m=p)
    {
        for(p=0,i=n-j; i<n; i++)  y[p++]=i;
        for(i=0; i<n; i++)  if(sa[i]>=j)  y[p++]=sa[i]-j;
        for(i=0; i<n; i++)  wv[i]=x[y[i]];
        for(i=0; i<m; i++)  wsf[i]=0;
        for(i=0; i<n; i++)  wsf[wv[i]]++;
        for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
        for(i=n-1; i>=0; i--)  sa[--wsf[wv[i]]]=y[i];
        t=x;x=y;y=t;
        x[sa[0]]=0;
        for(p=1,i=1; i<n; i++)
            x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
    }
}
int main()
{
    int n, cnt=0;
    scanf("%d",&n);
    scanf("%s",str);
    int k = 0;
    while(k<n && str[k]=='1') ++k;
    if(k==n) return 0*puts("0 0");
    for(int i=k;i<n;++i) s[cnt++] = str[i]-'0'+1;
    s[cnt] = 0;
    getsa(s,sa,cnt+1,3); 
    printf("%d %d\n",k,sa[cnt]);
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值