zoukankan      html  css  js  c++  java
  • [Usaco10Dec] Threatening Letter G

    Description

    给你一个长度为 (n) 的串 (s_1),再给你一个长度为 (m) 的串 (s_2),问需要至少多少个 (s_1) 的子串才可以拼成 (s_2)

    Solution

    (s_1) 建出 SAM,把 (s_2) 放到上面贪心地跑,如果能匹配就接着沿着 (trans) 边走,不匹配则回到根结点,并且 (ans+1)

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 2000005;
    int ans=1;
    struct SAM {
        int len[N], ch[N][26], fa[N], ind, last;
        int t[N], a[N], cnt[N], f[N];
        SAM() { ind = last = 1; }
        inline void extend(int id) {
            int cur = (++ ind), p;
            len[cur] = len[last] + 1;
            cnt[cur] = 1;
            for (p = last; p && !ch[p][id]; p = fa[p]) ch[p][id] = cur;
            if (!p) fa[cur] = 1;
            else {
                int q = ch[p][id];
                if (len[q] == len[p] + 1) fa[cur] = q;
                else {
                    int tmp = (++ ind);
                    len[tmp] = len[p] + 1;
                    for(int i=0;i<26;i++) ch[tmp][i] = ch[q][i];
                    fa[tmp] = fa[q];
                    for (; p && ch[p][id] == q; p = fa[p]) ch[p][id] = tmp;
                    fa[cur] = fa[q] = tmp;
                }
            }
            last = cur;
        }
        int p=1;
        void go(int id)
        {
            if(ch[p][id]) p=ch[p][id];
            else p=ch[1][id], ++ans;
        }
    } sam;
    
    int main() {
        ios::sync_with_stdio(false);
        string str,tmp;
        int n,m;
        cin>>n>>m;
        while(str.length()<n)
        {
            cin>>tmp;
            str+=tmp;
        }
        for(int i=0;i<n;i++) sam.extend(str[i]-'A');
        str="";
        while(str.length()<m)
        {
            cin>>tmp;
            str+=tmp;
        }
        for(int i=0;i<m;i++) sam.go(str[i]-'A');
        cout<<ans<<endl;
    }
    
    
    
  • 相关阅读:
    ITIL 4Foundation认证
    Linux服务器安全之 fail2ban的安装与配置
    Linux的常用基础命令
    jQuery源码学习(2):选择器初窥
    jQuery源码学习(1):整体架构
    从字符串拼接看JS优化原则
    理解函数作用域与闭包
    JavaScript DOM节点操作总结
    函数声明与函数表达式、变量提升
    CSS长度单位详解
  • 原文地址:https://www.cnblogs.com/mollnn/p/13283082.html
Copyright © 2011-2022 走看看