[USACO12JAN]视频游戏的连击Video Game Combos

本文介绍了一种使用 AC 自动机结合动态规划解决字符串匹配问题的方法。通过构建 AC 自动机加速多个模式串在文本中的搜索,并利用动态规划求解最优匹配方案。文中详细解释了 AC 自动机的建立、获取失败指针的过程以及动态规划的状态转移方程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

很早之前就做过啦

补一下题解

F(i,j)前i个的字符为j的匹配

注意end要累加

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int N=1e5+100;
int n,m;
struct AC_AUTO{
    struct Node{
        int vis[26];
        int fail;
        int end;
    }AC[N];
    int cnt;
    inline void Clear(int p){
        memset(AC[p].vis,0,sizeof(AC[p].vis));
        AC[p].end=0;
        AC[p].fail=0;
    }
    inline void Build(string S){
        int R=S.length();
        int now=0;
        for(int i=0;i<R;i++){
            if(!AC[now].vis[S[i]-'A']){
                cnt++;
                Clear(cnt);
                AC[now].vis[S[i]-'A']=cnt;
            }
            now=AC[now].vis[S[i]-'A'];
        }
        AC[now].end++;
    }
    inline void Get_Fail(){
        queue<int> Q;
        for(int i=0;i<3;i++){
            if(AC[0].vis[i]){
                AC[AC[0].vis[i]].fail=0;
                Q.push(AC[0].vis[i]);
            }
        }
        while(!Q.empty()){
            int x=Q.front();
//			cout<<x<<" ";
            Q.pop();
            for(int i=0;i<3;i++){
                if(AC[x].vis[i]){
                    AC[AC[x].vis[i]].fail=AC[AC[x].fail].vis[i];	
                    Q.push(AC[x].vis[i]);
                }
                else AC[x].vis[i]=AC[AC[x].fail].vis[i];
            }
            AC[x].end+=AC[AC[x].fail].end;
//			cout<<AC[x].end<<'\n';
        }
    }
    int f[1001][1001];
    inline void DP(){
        int ans=0;
        memset(f,-1,sizeof(f));
        f[0][0]=0;
        for(int i=1;i<=m;i++){
            for(int j=0;j<=cnt;j++){
                if(f[i-1][j]==-1)continue;
                for(int k=0;k<3;k++){
                    f[i][AC[j].vis[k]]=max(f[i][AC[j].vis[k]],f[i-1][j]+AC[AC[j].vis[k]].end);
                }
            }
        }
        for(int i=0;i<=cnt;i++){
            ans=max(ans,f[m][i]);
        }
        cout<<ans;
    }
}ACM;
int main(){
//	freopen("3119.in","r",stdin);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        string s;
        cin>>s;
        ACM.Build(s);
    }
    ACM.Get_Fail();
    ACM.DP();
}

### USACO 2016 January Contest Subsequences Summing to Sevens Problem Solution and Explanation In this problem from the USACO contest, one is tasked with finding the size of the largest contiguous subsequence where the sum of elements (IDs) within that subsequence is divisible by seven. The input consists of an array representing cow IDs, and the goal is to determine how many cows are part of the longest sequence meeting these criteria; if no valid sequences exist, zero should be returned. To solve this challenge efficiently without checking all possible subsequences explicitly—which would lead to poor performance—a more sophisticated approach using prefix sums modulo 7 can be applied[^1]. By maintaining a record of seen remainders when dividing cumulative totals up until each point in the list by 7 along with their earliest occurrence index, it becomes feasible to identify qualifying segments quickly whenever another instance of any remainder reappears later on during iteration through the dataset[^2]. For implementation purposes: - Initialize variables `max_length` set initially at 0 for tracking maximum length found so far. - Use dictionary or similar structure named `remainder_positions`, starting off only knowing position `-1` maps to remainder `0`. - Iterate over given numbers while updating current_sum % 7 as you go. - Check whether updated value already exists inside your tracker (`remainder_positions`). If yes, compare distance between now versus stored location against max_length variable's content—update accordingly if greater than previous best result noted down previously. - Finally add entry into mapping table linking latest encountered modulus outcome back towards its corresponding spot within enumeration process just completed successfully after loop ends normally. Below shows Python code implementing described logic effectively handling edge cases gracefully too: ```python def find_largest_subsequence_divisible_by_seven(cow_ids): max_length = 0 remainder_positions = {0: -1} current_sum = 0 for i, id_value in enumerate(cow_ids): current_sum += id_value mod_result = current_sum % 7 if mod_result not in remainder_positions: remainder_positions[mod_result] = i else: start_index = remainder_positions[mod_result] segment_size = i - start_index if segment_size > max_length: max_length = segment_size return max_length ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值