即日起在codingBlog上分享您的技术经验即可获得积分,积分可兑换现金哦。

[置顶] 字符串模板

编程语言 ypxrain 10℃ 0评论
本文目录
[隐藏]

1.KMP

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define N 1000010
#define INF 0x7fffffff
#define sqr(x) ((x) * (x))
#define pi acos(-1)

char s1[N], s2[N];
int next[N];

int l1,l2,ans[N];

int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9')  {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}

void pre()
{
    next[1] = 0; int k = 0;
    for (int i = 2; i <= l2; i++)
    {
        while (k && s2[i] != s2[k + 1]) k = next[k];
        if (s2[k] == s2[k + 1]) k++;
        next[i] = k;
    }
}

void kmp()
{
    int k = 0;
    for (int i = 1; i <= l1; i++)
    {
        while (k & s1[i] != s2[k + 1]) k = next[k];
        if (s1[i] == s2[k + 1]) k++;
        if (k == l2)
        {
            ans[++n] = i - k + 1;
            k = next[k];
        }
    }
}

int main()
{
    scanf("%s%s", s1 + 1, s2 + 1);
    l1 = strlen(s1 + 1); l2 = strlen(s2 + 1);
    pre();
    kmp();
    for (int i = 1; i <= n; i++)
        printf("%d ",ans[i]);
    printf("\n");
}

2.manacher

例题 HDU 3068

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define N 110005
#define INF 0x7fffffff
#define sqr(x) ((x) * (x))
#define pi acos(-1)

char ch[N << 1];
int len[N << 1];

int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9')  {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}

void manacher(char s[], int l)
{
    int L = 0;
    ch[L++] = '$';
    ch[L++] = '#';
    for (int i = 0; i < l; i++)
        ch[L++] = s[i], ch[L++] = '#';
    len[L] = 0;
    int mx = 0, p = 0;
    for (int i = 0; i < L; i++)
    {
        len[i] = mx > i ? std::min(len[2 * p - i], mx - i) : 1;
        while (ch[i + len[i]] == ch[i - len[i]]) len[i]++;
        if (i + len[i] > mx)
            mx = i + len[i], p = i;
    }
}

char s[N];

int main()
{
    while(~scanf("%s",s))  
    {  
        int L = strlen(s);  
        manacher(s, L);  
        int ans = 0;  
        for (int i = 0; i < 2 * L + 2; i++)  
        {  
            ans = std::max(ans, len[i] - 1);  
        }  
        printf("%d\n", ans);  
    }  
}

3.后缀数组

例题:BZOJ 3106

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define N 200005
#define INF 0x7fffffff
#define sqr(x) ((x) * (x))
#define pi acos(-1)

int len;
int b[N],c[N],d[N];
int sa[N],rank[N],height[N];

char s[N], ans[N];

int read()
{
    int x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}
    while (ch >= '0' && ch <= '9')  {x = x * 10 + ch - '0'; ch = getchar();}
    return x * f;
}

void getSa(int n,int m)
{
    for (int i = 0; i <= m; i++) b[i] = 0;
    for (int i = 1; i <= n; i++) b[s[i]]++;
    for (int i = 1; i <= m; i++) b[i] += b[i - 1];
    for (int i = n; i >= 1; i--) c[b[s[i]]--] = i;
    int t = 0;
    for (int i = 1; i <= n; i++)
    {
        if (s[c[i]] != s[c[i - 1]])
            t++;
        rank[c[i]] = t;
    }
    int j = 1;
    while (j <= n)
    {
        for (int i = 0; i <= n; i++) b[i] = 0;
        for (int i = 1; i <= n; i++) b[rank[i + j]]++;
        for (int i = 1; i <= n; i++) b[i] += b[i - 1];
        for (int i = n; i >= 1; i--) c[b[rank[i + j]]--] = i;
        for (int i = 0; i <= n; i++) b[i] = 0;
        for (int i = 1; i <= n; i++) b[rank[i]]++;
        for (int i = 1; i <= n; i++) b[i] += b[i - 1];
        for (int i = n; i >= 1; i--) d[b[rank[c[i]]]--] = c[i];
        t = 0;
        for (int i = 1; i <= n; i++)
        {
            if (rank[d[i]] != rank[d[i - 1]] || rank[d[i]] == rank[d[i - 1]] && rank[d[i - 1] + j] != rank[d[i] + j])
                t++;
            c[d[i]] = t;
        }
        for (int i = 1; i <= n; i++) rank[i] = c[i];
        if (t == n)
            break;
        j *= 2;
    }
    for (int i = 1; i <= n; i++)
        sa[rank[i]] = i;
}

void getHeight(int n)
{
    int k = 0;
    for (int i = 1; i <= n; i++)
    {
        if (k) k--;
        int j = sa[rank[i] - 1];
        while (i + k <= n && j + k <= n && s[i + k] == s[j + k])
            k++;
        height[rank[i]] = k;
    }
}

int main()
{
    scanf("%s",s);
    int len = strlen(s), m = 0;
    for (int i = len; i >= 1; i--)
    {
        m = std::max(m,(int)s[i]);
        s[i + len] = s[i] = s[i - 1]; 
    }
    getSa(len * 2, m);
    getHeight(len * 2);
    int j = 0;
    for (int i = 1; i <= len * 2; i++)
        if (sa[i] <= len)
            ans[++j] = s[sa[i] + len - 1];
    for (int i = 1; i <= len; i++)
        printf("%c",ans[i]);
    printf("\n");
}

转载请注明:CodingBlog » [置顶] 字符串模板

喜欢 (0)or分享 (0)
发表我的评论
取消评论

*

表情