SNOI2019 string

Subject description:

luogu

answer:

This question $ O (n) $.

First, the consecutive identical characters lump together.

Then consider the lexicographical comparison is bit by bit from front to back ratio, so:

When $ a [i]> a [i + 1] $, the delete $ a [i] $ than a certain puncturing $ a [j] (j> i) $ better;

When $ a [i] <a [i + 1] $, the delete $ a [j] $ than a certain puncturing $ a [i] $ better.

It is then deep search $ O (n) $.

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 1000050;
char a[N];
int b[N],c[N],n,m;
void dfs(int i)
{
    if(i>m)return ;
    if(c[i]>c[i+1])
    {
        for(int j=b[i];j<b[i+1];j++)printf("%d ",j);
        dfs(i+1);
    }else
    {
        dfs(i+1);
        for(int j=b[i];j<b[i+1];j++)printf("%d ",j);
    }
}
int main()
{
    scanf("%d%s",&n,a+1);
    for(int i=1;i<=n;i++)
        if(a[i]!=a[i-1])
            b[++m]=i,c[m]=a[i];
    b[++m] = n+1;
    dfs(1);
    puts("");
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/LiGuanlin1124/p/11130122.html