[USACO17DEC]Standing Out from the Herd

[USACO17DEC]Standing Out from the Herd

题目描述

Just like humans, cows often appreciate feeling they are unique in some way. Since Farmer John's cows all come from the same breed and look quite similar, they want to measure uniqueness in their names.

Each cow's name has some number of substrings. For example, "amy" has substrings {a, m, y, am, my, amy}, and "tommy" would have the following substrings: {t, o, m, y, to, om, mm, my, tom, omm, mmy, tomm, ommy, tommy}.

A cow name has a "uniqueness factor" which is the number of substrings of that name not shared with any other cow. For example, If amy was in a herd by herself, her uniqueness factor would be 6. If tommy was in a herd by himself, his uniqueness factor would be 14. If they were in a herd together, however, amy's uniqueness factor would be 3 and tommy's would be 11.

Given a herd of cows, please determine each cow's uniqueness factor.

定义一个字符串的「独特值」为只属于该字符串的本质不同的非空子串的个数。如 "amy" 与 “tommy” 两个串,只属于 "amy" 的本质不同的子串为 "a" "am" "amy" 共 3 个。只属于 "tommy" 的本质不同的子串为 "t" "to" "tom" "tomm" "tommy" "o" "om" "omm" "ommy" "mm" "mmy" 共 11 个。 所以 "amy" 的「独特值」为 3 ,"tommy" 的「独特值」为 11 。

给定 N (N \leq 10^5N105) 个字符集为小写英文字母的字符串,所有字符串的长度和小于 10^5105,求出每个字符串「独特值」。


sol

考虑广义sam

对于每一个点记录有多少串经过它。nsqrtn

然后再保利跳统计答案

复制代码
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#define maxn 20005
using namespace std;
int n,rt,la,cnt,f[maxn];
struct node{
    int par,Max,nex[26],num,now;
}s[maxn];
string ch[maxn];
void ins(int c){
    int p=la,np=++cnt;la=np;s[np].Max=s[p].Max+1;
    for(;p&&!s[p].nex[c];p=s[p].par)s[p].nex[c]=np;
    if(!p)s[np].par=rt;
    else {
        int q=s[p].nex[c],nq;
        if(s[q].Max==s[p].Max+1)s[np].par=q;
        else {
            nq=++cnt;s[nq].Max=s[p].Max+1;
            for(int j=0;j<26;j++)s[nq].nex[j]=s[q].nex[j];
            s[nq].par=s[q].par;s[q].par=s[np].par=nq;
            for(;p&&s[p].nex[c]==q;p=s[p].par)s[p].nex[c]=nq;
        }
    }
}
int main(){
    cin>>n;
    rt=la=cnt=1;
    for(int i=1;i<=n;i++){
        cin>>ch[i];int l=ch[i].length();
        la=rt;
        for(int j=0;j<l;j++){
            ins(ch[i][j]-'a');
        }
    }
    for(int i=1;i<=n;i++){
        int l=ch[i].length();
        int k=rt;
        for(int j=0;j<l;j++){
            k=s[k].nex[ch[i][j]-'a'];
            int p=k;
            for(;p&&s[p].now!=i;p=s[p].par)s[p].now=i,s[p].num++;
        }
    }
    for(int i=1;i<=cnt;i++)s[i].now=0;
    for(int i=1;i<=n;i++){
        int l=ch[i].length();
        int k=rt;long long ans=0;
        for(int j=0;j<l;j++){
            k=s[k].nex[ch[i][j]-'a'];
            int p=k;
            for(;p&&s[p].now!=i&&s[p].num==1;p=s[p].par)s[p].now=i,ans=ans+s[p].Max-s[s[p].par].Max;    
        }
        printf("%lld\n",ans);
    }
    return 0;
}
复制代码

 

posted @   liankewei123456  阅读(186)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示