美文网首页
牛客假日团队赛18

牛客假日团队赛18

作者: 云中翻月 | 来源:发表于2019-10-13 16:52 被阅读0次

    比赛链接
    https://ac.nowcoder.com/acm/contest/1089#question
    B题题解
    题意:给定两个大写字符组成的字符串a和b,求最少从a中选出多少个子串能够拼成b。
    对a构造SAM,然后顺序扫描b,从SAM的当前状态开始转移,如果存在转移边,就继续。否则就回到当前指向的b中字符对应的初始状态并将答案+1。
    代码如下

    /*
    
    */
    #define method_1
    #ifdef method_1
    /*
    题意:给定两个大写字符组成的字符串a和b,求最少从a中选出多少个子串能够拼成b。 
    对a构造SAM,然后顺序扫描b,从SAM的当前状态开始转移,如果存在转移边,就继续。否则就回到当前指向的b中字符对应的初始状态并将答案+1。 
    */
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstring>
    #include<cstdlib>
    #include<iomanip>
    #include<ctime>
    #include<string>
    #include<bitset>
    #define D(x) cout<<#x<<" = "<<x<<"  "
    #define E cout<<endl
    using namespace std;
    typedef long long ll;
    typedef pair<int,int>pii;
    const int maxn=50000+5;
    const int maxp=26+5;
    const int INF=0x3f3f3f3f;
    int n,m;
    int ans=1; //至少有一次 
    int read(){
        char ch=getchar();
        while(!isalpha(ch)) ch=getchar();
        return ch-'A';
    }
    struct Suffix_Automaton{
        int cnt,last,g[maxn<<2][maxp],NOW,pa[maxn<<2],len[maxn<<2];
        inline void init(){
            cnt=last=1;NOW=1;
        }
        inline void insert(int ch){
            int np=++cnt,pos=last;last=cnt;len[np]=len[pos]+1;
            for (;pos&&!g[pos][ch];pos=pa[pos]) g[pos][ch]=np;
            if (!pos) pa[np]=1;
            else{
                int posx=g[pos][ch];
                if (len[pos]+1==len[posx]) pa[np]=posx;
                else{
                    int q=++cnt;
                    len[q]=len[pos]+1;
                    for (int i=0;i<26;i++) g[q][i]=g[posx][i];
                    pa[q]=pa[posx];pa[posx]=pa[np]=q;
                    for (;g[pos][ch]==posx;pos=pa[pos]) g[pos][ch]=q;
                }
            }
        }
    }sa;
    void solve(){
        for(int i=1;i<=m;i++){
            int ch=read();
            if (sa.g[sa.NOW][ch]) sa.NOW=sa.g[sa.NOW][ch];//如果匹配继续跑
            else{ //如果不匹配了
                ans++;
                sa.NOW=sa.g[1][ch];
            }
        }
    }
    int main() {
    //  ios::sync_with_stdio(false);
        //freopen("Threatening Letter.in","r",stdin);
        scanf("%d%d",&n,&m);
        sa.init();
        for(int i=1;i<=n;i++) sa.insert(read());
        solve();
        printf("%d",ans);
        return 0;
    }
    #endif
    #ifdef method_2
    /*
    
    */
    
    #endif
    #ifdef method_3
    /*
    
    */
    
    #endif
    

    I题题解
    题解链接 https://www.luogu.org/problemnew/solution/P3000
    代码如下

    /*
    题解链接 https://www.luogu.org/problemnew/solution/P3000 
    */
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstring>
    #include<cstdlib>
    #include<iomanip>
    #include<ctime>
    #include<string>
    #include<bitset>
    #define D(x) cout<<#x<<" = "<<x<<"  "
    #define E cout<<endl
    using namespace std;
    typedef long long ll;
    typedef pair<int,int>pii;
    const int maxn=100000+5;
    const int INF=0x3f3f3f3f;
    int n,s; 
    struct node{
        int from,to;
    }edge[maxn<<1];
    int head[maxn],tot=1;
    void add(int from,int to){
        edge[++tot].from=head[from],head[from]=tot,edge[tot].to=to;
    }
    int cnt; //切断次数
    int d[maxn];//d[i]表示在以i为根的子树中,到叶子节点的最远距离(不包括被切断的部分)(单位为该路径上的边数+1) 
    void init(){
        cnt=0;
        memset(d,0,sizeof(d));
    }
    void dfs(int x,int fa,int mid){
        if(cnt>s) return;
        int maxd=0;
        for(int i=head[x];i;i=edge[i].from){
            int y=edge[i].to;
            if(y==fa) continue;
    //      D(x);D(y);E;
            dfs(y,x,mid);
            if(maxd+d[y]>mid){
    //          D(x);D(y);E;
                cnt++;
                maxd=min(maxd,d[y]);
            }
            else{
                maxd=max(maxd,d[y]);
            }
        }
        d[x]=maxd+1;
    //  D(x);D(d[x]);E;
    } 
    bool check(int mid){
        init();
        dfs(1,0,mid);
    //  D(mid);D(cnt);E;
        return cnt<=s;
    }
    int main() {
    //  ios::sync_with_stdio(false);
        //freopen("Cow Calisthenics.in","r",stdin);
        scanf("%d%d",&n,&s);
        int from,to;
        for(int i=1;i<=n-1;i++){
            scanf("%d%d",&from,&to);
            add(from,to),add(to,from);
    //      D(from);D(to);E;
        }
        check(3);
        int l=0,r=INF;
        while(l<r){
            int mid=l+r>>1;
            if(check(mid)==false) l=mid+1;
            else r=mid;
        }
        printf("%d",l);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:牛客假日团队赛18

          本文链接:https://www.haomeiwen.com/subject/cotkmctx.html