0
点赞
收藏
分享

微信扫一扫

yandex资格赛e(tire+bit+离线处理+二分)


这个姿势有点特殊。。

首先先把字符串变成数字再说。。用tire轻松解决。。。

然后就是要在区间内查询是否m个数都存在了。。

可以先离线处理,将之前出现过的相同的数字记下,枚举r,枚举的同时相同的数字只保留最后一个,因为区间右端已经确定,如果从右往左扫,之前的相同数字是起了重复作用,将这些数字标记为1的话就是转变为查询和为m的最小区间,直接二分即可。。由于是单点修改,区间和可用BIT维护。。

算是相当综合的一道题了。。。查询区间存在m个不同数的姿势get






/**
*        ┏┓    ┏┓
*        ┏┛┗━━━━━━━┛┗━━━┓
*        ┃       ┃  
*        ┃   ━    ┃
*        ┃ >   < ┃
*        ┃       ┃
*        ┃... ⌒ ...  ┃
*        ┃       ┃
*        ┗━┓   ┏━┛
*          ┃   ┃ Code is far away from bug with the animal protecting          
*          ┃   ┃ 神兽保佑,代码无bug
*          ┃   ┃           
*          ┃   ┃       
*          ┃   ┃
*          ┃   ┃           
*          ┃   ┗━━━┓
*          ┃       ┣┓
*          ┃       ┏┛
*          ┗┓┓┏━┳┓┏┛
*           ┃┫┫ ┃┫┫
*           ┗┻┛ ┗┻┛
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define eps 1e-8
#define succ(x) (1<<x)
#define lowbit(x) (x&(-x))
#define sqr(x) ((x)*(x))
#define mid (x+y>>1)
#define NM 100005
#define nm 2000005
#define pi 3.1415926535897931
using namespace std;
const ll inf=1e9;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f*x;
}



struct node{
node*c[26];
vector<int>a;
}N[nm],*root=N,*o=N;
int n,m,a[NM],pre[NM];
ll ans;
char _s[15];

void add(int x,int t){for(;x<=n;x+=lowbit(x))a[x]+=t;}
int sum(int x,int s=0){for(;x;x-=lowbit(x))s+=a[x];return s;}

bool check(int x,int y){return sum(y)-sum(x-1)>=m;}

int main(){
//freopen("data.in","r",stdin);
n=read();m=read();
inc(i,1,n){
scanf("%s",_s);node*r=root;
inc(j,0,strlen(_s)-1){
if(!r->c[_s[j]-'a'])r->c[_s[j]-'a']=++o;
r=r->c[_s[j]-'a'];
}
r->a.push_back(i);
}
inc(i,1,m){
scanf("%s",_s);node*r=root;
inc(j,0,strlen(_s)-1)if(!r->c[_s[j]-'a'])return 0*printf("0\n");else r=r->c[_s[j]-'a'];
inc(j,0,r->a.size()-1)if(j)pre[r->a[j]]=r->a[j-1];else pre[r->a[j]]=-1;
}
inc(i,1,n){
if(pre[i])add(i,1);if(pre[i]>0)add(pre[i],-1);int s=0;
for(int l=1,r=i,t=l+r>>1;l<=r;t=l+r>>1)
if(check(t,i)){s=t;l=t+1;}else r=t-1;
ans+=s;
}
printf("%lld\n",ans);
return 0;
}





题目链接:

​​https://contest.yandex.com/algorithm2018/contest/7491/problems/E/?success=10691722#215/2018_02_16/gsL8Qr3ZsY​​



Good Snippets

Time limit

3 seconds

Memory limit

512Mb

Input

standard input or input.txt

Output

standard output or output.txt



One of the most important component of a good search engine is the informative snippets of the documents.

Snippet management is a feature of some text editors, program source code editors, IDEs, and related software. It allows the user to avoid repetitive typing in the course of routine edit operations.

Vladimir is working on a snippets project. Vladimir's algorithm selects the most appropriate snippet for the document, but it only considers such parts of the document that contain words that occur in the query.

You are given a sequence of words w1, w2, …, wn and the set of words s1, s2, …, sk (different words from the query). Find the number of pairs of indices (l, r) (1 ≤ l ≤ r ≤ n) such that among the words wl, wl+1, …, wr there are all query words si. We say that word si occurs on the segment from l to r if and only if there exits j, such that wj = si and l ≤ j ≤ r. That is, we count only exact matches and do not consider substrings.



Input format



The first input line contains two integers n and k (1 ≤ n, k ≤ 100 000), the number of words in the document and the number of words in the query, respectively. The second line contains n words wi (1 ≤ |wi| ≤ 10). The third line contains k different words si (1 ≤ |si| ≤ 10).

The words in the lines are separated by single spaces. The words consist of lowercase English letters.



Output format



In a single line print the number of pairs of indices satisfying the problem.



Sample 1

Input

Output

6 2yandex search engine algorithm is powerfulyandex algorithm

3

Sample 2

Input

Output

9 1the best solution is trying to find better solutionsolution

27

Sample 3

Input

Output

7 2a b a c a b ab a

18



yandex资格赛e(tire+bit+离线处理+二分)_#include


举报

相关推荐

0 条评论