Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) F. Substrings in a String(暴力?)
2018-01-24 17:09
531 查看
题目链接:http://codeforces.com/contest/914/problem/F
思路很像shift-and啊?直接bitset记录主串每个字符出现的位置,然后对于每个询问暴力匹配,复杂度O(n^2/32)
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
bitset<MAXN> x[26],ans,one;
char s[MAXN],y[MAXN],c[2];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++)
{
one.set(i);
x[s[i]-'a'].set(i);
}
int q;
scanf("%d",&q);
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int i;
scanf("%d%s",&i,c);
x[s[i]-'a'].reset(i);
s[i]=c[0];
x[s[i]-'a'].set(i);
}
if(op==2)
{
int l,r;
scanf("%d%d%s",&l,&r,y);
int len=strlen(y);
if(len>r-l+1)
{
puts("0");
continue;
}
ans=one;
for(int i=0;i<len;i++)
{
ans&=(x[y[i]-'a']>>i);
}
int res=(ans>>(l)).count()-(ans>>(r+2-len)).count();
printf("%d\n",res);
}
}
return 0;
}
思路很像shift-and啊?直接bitset记录主串每个字符出现的位置,然后对于每个询问暴力匹配,复杂度O(n^2/32)
代码:
#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5;
bitset<MAXN> x[26],ans,one;
char s[MAXN],y[MAXN],c[2];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%s",s+1);
int n=strlen(s+1);
for(int i=1;i<=n;i++)
{
one.set(i);
x[s[i]-'a'].set(i);
}
int q;
scanf("%d",&q);
while(q--)
{
int op;
scanf("%d",&op);
if(op==1)
{
int i;
scanf("%d%s",&i,c);
x[s[i]-'a'].reset(i);
s[i]=c[0];
x[s[i]-'a'].set(i);
}
if(op==2)
{
int l,r;
scanf("%d%d%s",&l,&r,y);
int len=strlen(y);
if(len>r-l+1)
{
puts("0");
continue;
}
ans=one;
for(int i=0;i<len;i++)
{
ans&=(x[y[i]-'a']>>i);
}
int res=(ans>>(l)).count()-(ans>>(r+2-len)).count();
printf("%d\n",res);
}
}
return 0;
}
相关文章推荐
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) E. Palindromes in a Tree(点分治)
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) .A-2018.1.20
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) D】Bash and a Tough Math Puzzle
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C. Travelling Salesman and Specia
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined)
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Specia
- CodeCraft-18 and Codeforces Round #458(Div. 1+Div. 2,combined)
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) A】 Perfect Squares
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined)C,D详解
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) B】 Conan and Agasa play a Card Game
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) C】 Travelling Salesman and Special Numbers
- Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) B题(博弈)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) B. Bash's Big Day(分拆素因子+求最多的公因子)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) -- B. Bash's Big Day (唯一分解定理)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) -- C. Felicity is Coming! (STL水过)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)
- Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C - Felicity is Coming!
- Codecraft-18 and Codeforces Round #458 (combined) D. Bash and a Tough Math Puzzle 线段树+二分+卡时间+优化