[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^5N≤105) 个字符集为小写英文字母的字符串,所有字符串的长度和小于 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;
}

原文地址:https://www.cnblogs.com/liankewei/p/12269949.html

时间: 2024-12-29 12:11:22

[USACO17DEC]Standing Out from the Herd的相关文章

POJ 2140 Herd Sums

http://poj.org/problem?id=2140 Description The cows in farmer John's herd are numbered and branded with consecutive integers from 1 to N (1 <= N <= 10,000,000).  When the cows come to the barn for milking, they always come in sequential order from 1

this.standing

this.standing水平走的时候是true,纵向有速度是false,斜面轻微纵向速度也true var accel = this.standing ? this.accelG: this.accelA; if (this.currentAnim === this.anims.run && !this.sounds.walk.looping) { this.sounds.walk.loop(); } if (this.currentAnim !== this.anims.run &am

hdu 2715 Herd Sums

Herd Sums Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 772    Accepted Submission(s): 375 Problem Description The cows in farmer John's herd are numbered and branded with consecutive integers

Where Are You Standing?

/*********************************************************************** * Where Are You Standing? * 说明: * 简单的画一下目前所接触到的东西网络结构 * * 2016-7-23 深圳 南山平山村 曾剑锋 **********************************************************************/

GCJ 2015Q(Standing Ovation-贪心)

Problem It's opening night at the opera, and your friend is the prima donna (the lead female singer). You will not be in the audience, but you want to make sure she receives a standing ovation -- with every audience member standing up and clapping th

Luogu P4122 [USACO17DEC]Blocked Billboard

Luogu P4122 [USACO17DEC]Blocked Billboard 解析 画图想一想,莫得想复杂咯! Code #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define LL long long using namespace std; struct matrix { int ax,ay,bx,

POJ 2140 Herd Sums 公式推导

题意:给出n<=1e7 求有多少个连续数之和等于k x+x+1+....x+k=n (k+1)k/2+(k+1)x=n (k+1)k+(k+1)2x=2*n (k+1)*(2x+k)=2*n    2*n为偶 k+1,2x+k都为2*n因子 &&一奇一偶 得到:2*n有多少个奇因子(或者偶因子)就有多少对解 (k,n固定可以得到首项x 得出一解) #include <iostream> #include <cstdio> #include <cmath&

standing

2bc*cosA=b^2+c^2-a^2 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> using namespace std; const double pi=3.1415926535898;//我不明白他为什么会卡精度 int t; double x1,y,r1,x2,y2,r2,s,a,b,c,k,m,w,p

BZOJ3941 : [Usaco2015 Feb]Fencing the Herd

若所有点同侧则表明将各个点带入直线解析式ax+by-c后得到的值均同号等价于最大值和最小值同号考虑CDQ分治,每一步分治的过程中求出上下凸壳,然后三分答案即可时间复杂度$O(n\log^2n)$ #include<cstdio> #include<algorithm> typedef long long ll; const int N=200010; const ll inf=1LL<<60; struct P{int x,y;P(){}P(int _x,int _y)