BZOJ4566&&lg3181 HAOI找相同字符(广义后缀自动机)

BZOJ4566&&lg3181 HAOI找相同字符(广义后缀自动机)

题面

自己找去

HINT

给定两个文本串,问从两个串中各取一个非空子串,使这俩子串相同,问方案有多少种。我的思路是建立一个广义后缀自动机,对于每个节点记录\(size[1]和size[2]\)分别表示这个节点接受的子串在A中出现多少次,在B中出现多少次,然后\(ans=\sum_{u=2}^{tot}{size[u][1]*size[u][2]*(node[u].len-node[fa].len)}\)注意要long long

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    int w=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        w=(w<<3)+(w<<1)+ch-48;
        ch=getchar();
    }
    return w*f;
}
int n,m;
int size[800010][3];
struct SUFFIXAUTOMATON{
    struct Node{
        int fa,len;
        map<int,int> ch;
    }node[800010];
    int lst,tot,root;
    inline void init(){
        lst=tot=root=1;return;
    }
    inline void extend(int now,int id){
        int p=lst;tot++;lst=tot;int np=tot;
        node[np].len=node[p].len+1;size[np][id]=1;
        while(p&&!node[p].ch[now]){
            node[p].ch[now]=np;
            p=node[p].fa;
        }
        if(!p) node[np].fa=1;
        else{
            int q=node[p].ch[now];
            if(node[q].len==node[p].len+1){
                node[np].fa=q;
            }
            else{
                int nq=++tot;node[nq]=node[q];
                node[nq].len=node[p].len+1;
                node[q].fa=nq;node[np].fa=nq;
                while(p&&node[p].ch[now]==q){
                    node[p].ch[now]=nq;
                    p=node[p].fa;
                }
            }
        }
    }
}SAM;
int cnt,head[800010];
struct Edge{
    int from,to,next;
}edge[1600010];
inline void addedge(int u,int v){
    cnt++;
    edge[cnt].from=u;
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
char ch[200010];
inline void dfs(int u){
    for(int i=head[u];i;i=edge[i].next){
        int v=edge[i].to;dfs(v);
        size[u][1]+=(long long)size[v][1];
        size[u][2]+=(long long)size[v][2];
    }
    return;
}
long long ans;
signed main(){
    SAM.init();
    for(int i=1;i<=2;i++){
        cin>>ch;int len=strlen(ch);
        for(int j=0;j<len;j++){
            SAM.extend(ch[j]-'a'+1,i);
        }
        SAM.lst=1;
    }
    for(int i=2;i<=SAM.tot;i++){
        addedge(SAM.node[i].fa,i);
    }
    dfs(1);
    //cout<<SAM.tot<<endl; 
    for(int i=2;i<=SAM.tot;i++){
        int f=SAM.node[i].fa;
        //cout<<(long long)size[i][1]*size[i][2]<<" "<<SAM.node[i].len-SAM.node[f].len<<endl;
        long long cur1=(long long)size[i][1]*size[i][2];long long cur2=(long long)SAM.node[i].len-SAM.node[f].len;
        //cout<<cur1<<" "<<cur2<<endl;
        ans+=(long long)cur1*cur2;
    }
    cout<<ans<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/wenci/p/10639566.html

BZOJ 2908 题目是一个数据下载任务。这个任务要求下载指定的数据文件,并统计文件中小于等于给定整数的数字个数。 为了完成这个任务,首先需要选择一个合适的网址来下载文件。我们可以使用一个网络爬虫库,如Python中的Requests库,来帮助我们完成文件下载的操作。 首先,我们需要使用Requests库中的get()方法来访问目标网址,并将目标文件下载到我们的本地计算机中。可以使用以下代码实现文件下载: ```python import requests url = '目标文件的网址' response = requests.get(url) with open('本地保存文件的路径', 'wb') as file: file.write(response.content) ``` 下载完成后,我们可以使用Python内置的open()函数打开已下载的文件,并按行读取文件内容。可以使用以下代码实现文件内容读取: ```python count = 0 with open('本地保存文件的路径', 'r') as file: for line in file: # 在这里实现对每一行数据的判断 # 如果小于等于给定整数,count 加 1 # 否则,不进行任何操作 ``` 在每一行的处理过程中,我们可以使用split()方法将一行数据分割成多个字符串,并使用int()函数将其转换为整数。然后,我们可以将该整数与给定整数进行比较,以判断是否小于等于给定整数。 最后,我们可以将统计结果打印出来,以满足题目的要求。 综上所述,以上是关于解决 BZOJ 2908 数据下载任务的简要步骤和代码实现。 希望对您有所帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值