NOIP-字符串

9 篇文章 0 订阅
3 篇文章 0 订阅

LA 4670 AC自动机

void pattern()
{
    memset(count, 0, sizeof(count));

    int sl = strlen(str);
    TrieNode *now = root;

    for(int i = 0; i < sl; i++)
    {
        now = now->p[str[i]-'a'];

        TrieNode *tmp = now;

        while(tmp != root)
        {
            count[tmp->end]++;
            tmp = tmp->fail;
        }
    }
}

void output()
{
    int maxcnt = 0;

    for(int i = 1; i <= n; i++)
    {
        for(int j = i + 1; j <= n; j++)
            if(!strcmp(c[i], c[j]))
                count[i] = std::max(count[i], count[j]);
        maxcnt = std::max(count[i], maxcnt);        
    }

    write(maxcnt), puts("");

    for(int i = 1; i <= n; i++)
        if(count[i] == maxcnt)
            printf("%s\n",c[i]);
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("4670.in","r",stdin);
    freopen("4670.out","w",stdout);
#endif

    while(true)
    {
        read(n);

        if(!n) break;

        init(), build();

        pattern();

        output();
    }

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

UVa 11019 字符串哈希


typedef unsigned long long ull; 
typedef unsigned int uint;

const int maxn = 1005, maxm = 1005;
const uint Base = 27;
const ull Basell = 29;

int n, m, x, y;
char P[maxn][maxm], T[maxn][maxm];

ull hashll[maxn][maxm], movell[maxm], rowll[maxn];
uint hash[maxn][maxm], move[maxm], row[maxn];


void init()
{
    read(n), read(m);
    for(int i = 1; i <= n; i++)
        scanf("%s",P[i] + 1);
    read(x), read(y);
    for(int i = 1; i <= x; i++)
        scanf("%s",T[i] + 1);
}
void prework()
{   
    uint *arr;
    ull *arrll;
    char *str;

    move[0] = movell[0] = 1;
    for(int i = 1; i <= m; i++)
    {
        move[i] = move[i-1]*Base;
        movell[i] = movell[i-1]*Basell;
    }

    for(int i = 1; i <= n; i++)
    {
        str = P[i], arr = hash[i], arrll = hashll[i];

        for(int j = 1; j <= m; j++)
        {
            arr[j] = arr[j-1]*Base + str[j] - 'a';
            arrll[j] = arrll[j-1]*Basell + str[j] - 'a';
        }
    }

    for(int i = 1; i <= x; i++)
    {
        row[i] = rowll[i] = 0;

        for(int j = 1; j <= y; j++)
        {
            row[i] *= Base, row[i] += T[i][j] - 'a';
            rowll[i] *= Basell, rowll[i] += T[i][j] - 'a';
        }
    }
}
int solve()
{
    int ret = 0;

    for(int i = 0; i + x <= n; i++)
        for(int j = 0; j + y <= m; j++)
        {
            bool flag = true;
            if(i == 0 && j == 1)
                i++, i--;
            for(int k = 1; k <= x && flag; k++)
            {
                uint t = hash[i + k][j + y] - hash[i + k][j]*move[y];
                ull tll = hashll[i + k][j + y] - hashll[i + k][j]*movell[y];
                if(t != row[k] || tll != rowll[k]) flag = false;
            }

            if(flag) ret++;
        }
    return ret; 
}
int main()
{
    int T;

#ifndef ONLINE_JUDGE
    freopen("11019.in","r",stdin);
    freopen("11019.out","w",stdout);
#endif

    read(T);

    while(T--)
    {
        init(), prework();

        write(solve()), puts("");
    }

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;       
}

Bzoj 1030 AC自动机DP
计算母串包含任意模板串的方案数

母串包含任意模板串的方案数 = 方案总数 - 母串不包含任意模板串的方案数

void update(int len)
{   
    for(int x = 1; x <= tot; x++) 
        for(int i = 0; i < size; i++)
            if(!T[x].p[i]->end)
            {
                T[x].p[i]->dp[len] += T[x].dp[len-1];
                T[x].p[i]->dp[len] %= Mod;
            }
}
long long getans()
{
    long long ans = 0, sum = 1;

    root->dp[0] = 1;
    for(int i = 1; i <= m; i++) update(i);

    for(int i = 1; i <= tot; i++)
        ans += T[i].dp[m], ans %= Mod;
    for(int i = 1; i <= m; i++)
        sum *= size, sum %= Mod;

    ans = (sum - ans + Mod)%Mod;

    return ans; 
}

UVa 11468 AC自动机DP

计算母串不包含任意模板串的概率

void update(int len)
{   
    for(int x = 1; x <= tot; x++) 
        for(int i = 0; i < size; i++)
            if(!T[x].p[i]->end && hash[i])
                T[x].p[i]->dp[len] += T[x].dp[len-1]*v[i];          
}

long double getans()
{
    long double ans = 0;

    for(int i = 1; i <= tot; i++) ans += T[i].dp[m];

    return ans; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值