[JSOI2007]文本生成器 AC自动机

https://www.luogu.org/problemnew/show/P4052

https://ac.nowcoder.com/acm/problem/20155

中文题,相信大家都读得懂;

首先要求出现过的串的总数,就用总数,减去,没有出现过的,既然是几个串,那就是AC自动机,加矩阵快速幂,但不过这道题由于文本比较长所以就只有用dp刚刚开始想着用类似的数位dp的办法做,只能过一半的样列,后来暴力写,也只能过一半,最后发现fail没有更新下去。改了后,前面的类似数位dp 的还是没有过,后面的倒是过了,可能想错了吧。

dp[m][i]表示取了m位,第m位i的方法。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=10007;
const int N=6000+10;
struct Aho_Trie
{
    int nxt[N][26],fail[N],endd[N];
    int l,root;
    int newnode()
    {
        endd[l]=fail[l]=0;
        memset(nxt[l],0,sizeof(nxt[l]));
        return l++;
    }
    void init()
    {
        l=0;
        root=newnode();
    }
    void Insert(char *s)
    {
        int len=strlen(s),u=root;
        for(int i=0;i<len;i++)
        {
            int x=s[i]-'A';
            if(nxt[u][x]==0) nxt[u][x]=newnode();
            u=nxt[u][x];
        }
        endd[u]=1;
    }
    void build()
    {
        queue<int>qu;
        for(int i=0;i<26;i++)
            if(nxt[root][i]!=0) qu.push(nxt[root][i]);
        while(!qu.empty())
        {
            int u=qu.front();qu.pop();
            endd[u]+=endd[fail[u]];
            for(int i=0;i<26;i++)
            {
                if(nxt[u][i]) fail[nxt[u][i]]=nxt[fail[u]][i],qu.push(nxt[u][i]);
                else nxt[u][i]=nxt[fail[u]][i];
            }
        }
    }
}ac;
int n,m,dp[110][N];
char tmp[110];

int quick(int a,int n)
{
    int ans=1;
    while(n)
    {
        if(n&1)
            ans=ans*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    ac.init();
    for(int i=1;i<=n;i++)
    {
        cin>>tmp;
        ac.Insert(tmp);
    }
    ac.build();
    int cnt=ac.l-1;
    ///cout<<cnt<<endl;
    memset(dp,0,sizeof(dp));
    dp[0][0]=1;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<=cnt;j++)
        {
            for(int k=0;k<26;k++)
            {
                if(!ac.endd[ac.nxt[j][k]])
                    dp[i+1][ac.nxt[j][k]]=(dp[i+1][ac.nxt[j][k]]+dp[i][j])%mod;
            }
        }
    }
    int ans=quick(26,m);
    for(int i=0;i<=cnt;i++)
    {
        ans=(ans-dp[m][i]+mod)%mod;
        ///cout<<dp[m][i]<<endl;
    }
    cout<<ans<<endl;
    return 0;
}

 

### JSOI 星球大战 相关题目及解法 #### 题目背景 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系。反抗军正在计划一次大规模的反攻行动[^2]。 #### 题目描述 给定一张图表示星系中的行星及其连接关系,每颗行星可以看作是一个节点,而边则代表两颗行星之间的通信通道。初始时所有行星都是连通的。然而,随着时间推移,某些行星可能被摧毁,从而影响到整体网络的连通性。每次询问需要返回当前还剩下多少个连通分量。 该问题的核心在于动态维护图的连通性变化情况,并快速响应查询操作。 --- #### 解决方案概述 此问题可以通过 **并查集 (Disjoint Set Union, DSU)** 数据结构来高效解决。以下是具体实现方法: 1. 并查集是一种用于处理不相交集合的数据结构,支持两种主要操作: - `find(x)`:找到元素 $x$ 所属集合的根节点。 - `union(x, y)`:将两个不同集合合并成一个新的集合。 这些操作的时间复杂度接近常数级别(通过路径压缩优化后为 $\alpha(n)$),其中 $\alpha(n)$ 是阿克曼函数的逆函数。 2. 对于本题而言,由于是倒序模拟行星毁灭的过程,因此可以从最终状态向前回溯重建历史记录。即先假设所有的行星都被摧毁了,再逐步恢复它们的存在状态。 3. 使用数组存储每个时间点上的事件顺序,按照输入数据给出的销毁次序依次执行相应的动作即可完成任务需求。 --- #### 实现细节 下面提供了一个基于 Python 的解决方案框架: ```python class UnionFind: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, x): if self.parent[x] != x: self.parent[x] = self.find(self.parent[x]) # 路径压缩 return self.parent[x] def union_set(self, x, y): xr = self.find(x) yr = self.find(y) if xr == yr: return False if self.rank[xr] < self.rank[yr]: self.parent[xr] = yr elif self.rank[xr] > self.rank[yr]: self.parent[yr] = xr else: self.parent[yr] = xr self.rank[xr] += 1 return True def main(): import sys input = sys.stdin.read data = input().split() N, M = int(data[0]), int(data[1]) edges = [] for i in range(M): u, v = map(int, data[i*2+2:i*2+4]) edges.append((u-1, v-1)) # Convert to zero-based index destroyed_order = list(map(lambda x:int(x)-1, data[M*2+2:M*2+N+2])) queries = [] uf = UnionFind(N) current_components = N result = [] # Preprocess the reverse order of destructions. active_edges = set(edges) edge_map = {tuple(sorted(edge)): idx for idx, edge in enumerate(edges)} status = [True]*M for planet in reversed(destroyed_order): initial_state = current_components connected_to_planet = [ e for e in active_edges if planet in e and all(status[edge_map[tuple(sorted(e))]] for e in active_edges)] for a, b in connected_to_planet: if uf.union_set(a, b): current_components -= 1 result.append(current_components) queries.insert(0, str(initial_state)) print("\n".join(reversed(result))) if __name__ == "__main__": main() ``` 上述代码定义了一个简单的并查集类以及主程序逻辑部分。它读取标准输入流中的数据,构建所需的邻接表形式表达图的关系矩阵;接着依据指定好的破坏序列逐一还原各阶段下的实际状况直至结束为止。 --- #### 性能分析 对于最大规模测试案例来说 ($N=10^5$, $M=4 \times 10^5$),这种方法能够很好地满足性能要求。因为每一次联合操作几乎都可以视为 O(α(N)) 时间消耗,所以总体运行效率非常高。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值