The minimum cover line C magical necklace

Mother's Day is coming, little H ready to give her a special necklace.
This string can be seen as a necklace with lowercase letters, lowercase letters each represent a color.

To make this necklace, small H purchased two machines.
The first machine may generate all forms of palindromic sequence, the second machine can be connected to two palindromic sequence, and the second machine there is a special property:
   If a suffix string and a string of prefix is exactly the same, then this can be repeated partially overlap. For example: aba and aca connected, the string may be generated abaaca or abaca.

Now given target style necklace, you need to ask how many times a second machine in order to generate this special necklace.

 

 

The minimum segment covering ... 

I think the difference is Fenwick tree interval modification ... 

Hack did not own ai ...

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
#define rel(i,x,y) for(ll i=(x);i<(y);i++)
#define rep(i,x,y) for(ll i=(x);i<=(y);i++)
#define red(i,x,y) for(ll i=(x);i>=(y);i--)
#define res(i,x) for(ll i=head[x];i;i=nxt[i])
using namespace std;

const ll N=1e5+5;
const ll Inf=1e18;

inline ll read() {
    ll x=0;char ch=getchar();bool f=0;
    while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
    return f?-x:x;
}
int R[120000];
char s[120000],S[120000];
struct node
{

    int x,y;

}a[120000];
int cnt=0;
int n;
int tot;
void init()
{
     tot=1;
    S[0]='@';
    S[1]='#';
    for(int i=1;i<=n;i++)
{
    S[++tot]=s[i];
    S[++tot]='#';
}
    S[++tot]='\0';

}
void mlc()
{

    int p=0;
    int maxlen=0;
    for(int i=1;i<tot;i++)
    {
        if(i<maxlen) R[i]=min(R[2*p-i],maxlen-i+1);
        else
        R[i]=1;
        while(S[i+R[i]]==S[i-R[i]]) R[i]++;
    
        {        
            a[++cnt].x=i-R[i]+1;
            a[cnt].y=i+R[i]-1;
        }
            if(i+R[i]-1>maxlen)
        {
                maxlen=i+R[i]-1;
                p=i;
        
        }
    }        
}

bool cmp(node aa,node b)
{
    if(aa.x==b.x)
    return aa.y>b.y;
    else
    return aa.x<b.x;

}
int main() {

    while(scanf("%s",s+1)!=EOF)
{
    
    for(int i=0;i<=n;i++)
{
    R[i]=0;

}
    n=strlen(s+1);
    cnt=0;
    int ans=0;
    init();
    mlc();
    sort(a+1,a+1+cnt,cmp);
    int l=a[1].y;
    int now=l;
    int j;
    int fla=0;int tes=0;
    if(a[1].y>=a[cnt].y) 
    {
        cout<<0;
        continue;

    }
    for(int i=2;i<=cnt;i++)
    {
        l=now;
        
            while(a[i].x<=l&&i<=cnt)
{
            
            now=max(now,a[i].y);i++;

}
    i--;
    if(now==l) break;
    else ans++;



}    cout<<ans<<endl;

}
}

 

Guess you like

Origin www.cnblogs.com/OIEREDSION/p/11349852.html