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; 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用\[1\],对于问题P7114 \[NOIP2020\] 字符串匹配,我们可以使用枚举的方法来确定AB的可能情况,并进一步确定C的可能情况。假设枚举的AB是前缀iii,那么C只可能是后缀i+1i+1i+1,以及后缀i+1i+1i+1去掉前面的若干AB后得到的后缀。我们可以预处理出每个前缀和后缀中出现奇数次字符的个数。根据观察,对于AB循环奇数次和偶数次来说,它们的C的上述值分别相同。因此,我们可以分别考虑AB循环奇数次和偶数次的情况,从前往后记录当前有多少个前缀的出现奇数次字符个数为i。在询问时,我们可以直接暴力询问前缀和,总的复杂度大约是O(26n)。如果使用树状数组,复杂度可以优化到O(nlog26)。 根据引用\[2\],这个问题与周期串有关。朴素的KMP算法只能求出一个周期串的最小循环节,对于本题来说是不够的。我们需要知道对于一个AB来说,它最多能往后循环多少次。虽然可以使用二分法来解决,但是复杂度较高。 根据引用\[3\],我们可以优化复杂度。由于postodd每次只变化1,我们可以记录从i变化到i+1或者i-1时,A串满足条件的个数的变化情况。对于奇数k,没有什么特殊的情况。对于偶数k,我们注意到C串的奇偶性与整串一致,因此可以直接使用整串的奇偶性即可。 综上所述,对于问题P7114 \[NOIP2020\] 字符串匹配,我们可以使用枚举的方法来确定AB的可能情况,并根据不同情况进行处理。可以使用树状数组来优化复杂度。 #### 引用[.reference_title] - *1* *2* [【LuoguP7114】[NOIP2020] 字符串匹配 (扩展KMP算法)](https://blog.csdn.net/element_hero/article/details/113036427)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [P7114 [NOIP2020] 字符串匹配](https://blog.csdn.net/beneath_haruhi/article/details/120634165)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值