四川第七届 C Censor (字符串哈希)
Posted 蔡军帅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四川第七届 C Censor (字符串哈希)相关的知识,希望对你有一定的参考价值。
Censor
frog is now a editor to censor so-called sensitive words (敏感词).
She has a long text pp. Her job is relatively simple -- just to find the first occurence of sensitive word ww and remove it.
frog repeats over and over again. Help her do the tedious work.
Input
The input consists of multiple tests. For each test:
The first line contains 11 string ww. The second line contains 11string pp.
(1≤length of w,p≤5?1061≤length of w,p≤5?106, w,pw,p consists of only lowercase letter)
Output
For each test, write 11 string which denotes the censored text.
Sample Input
abc
aaabcbc
b
bbb
abc
ab
Sample Output
a ab
题目大意;就是每组测试数据输入两个字符串;问第二个字符串中把第一个字符串一样的删除,问最后面还剩下什么;并输出;注意字符串删除后还会形成一个新的字符串;
#include<cstdio> #include<string.h> #include<vector> #include<queue> #include<stack> #include<cmath> #include<iostream> #include<algorithm> #include<deque> using namespace std; #define ll unsigned long long//要用长整型 char a[5000005]; char b[5000005]; ll p[5000005]; ll a131[5000005]; deque<ll>s; void init() {//先打表以防超时 a131[0]=1; for(ll i=1;i<=5000000;i++) { a131[i]=a131[i-1]*131; } } int main() { init(); while(~scanf("%s",&a)) { while(!s.empty()) s.pop_back(); memset(p,0,sizeof(p)); scanf("%s",&b); ll la=strlen(a); ll lb=strlen(b); ll ss=0; for(ll i=0;i<la;i++) {//对ss进行哈希 ss=(ss*131+a[i]); } ll cnt=1; for(ll i=0;i<lb;i++) { s.push_back(b[i]); if(s.size()==1) { p[cnt++]=b[i]; } else { p[cnt]=(p[cnt-1]*131+b[i]); cnt++; } if(s.size()>=la&&(p[cnt-1]-(p[cnt-la-1]*a131[la]))==ss)//选择la长度的哈希,要减去之前的 {//一旦发现哈希相同,删除相同字符串 for(ll j=1;j<=la;j++) { cnt--; s.pop_back(); } } } while(!s.empty()) { printf("%c",s.front()); s.pop_front(); } printf("\n"); } return 0; }
以上是关于四川第七届 C Censor (字符串哈希)的主要内容,如果未能解决你的问题,请参考以下文章
四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)