1 条题解
-
1
字符串题。这道题可以用哈希做。
把字符串看成一个 位进制数,先算得字符串 的hash值,再依次遍历字符串 ,维护它的前缀hash值 。
过程中将 的 长度为 的后缀 与 匹配
(hash匹配应该会的吧)。如果匹配成功,将位数减少即可。详见代码:
#include<iostream> #include<cstdio> using namespace std; inline int read_str(char *s){//这个快读可以快速读取字符串,同时返回字符串长度 s[0]=' '; int x=0; char c=getchar(); while (c<'a' || c>'z'){ c=getchar(); } while (c>='a'&&c<='z'){ s[++x]=c; c=getchar(); } return x; } char a[1000006],b[1000006],st[1000006];//st[i]是要输出的答案 int n,m,top; unsigned long long hash1,sum[1000006],bin[1000006];//用自然溢出hash,难以被卡 int main(){ n=read_str(a); m=read_str(b); bin[1]=1; for (int i=2;i<=n;i++){ bin[i]=bin[i-1]*13331ull;//让P=13331,这样难以冲突 } for (int i=1;i<=m;i++){ hash1=hash1+(b[i]-'a'+1)*bin[i];//计算B的hash } for (int i=1;i<=n;i++){ st[++top]=a[i]; sum[top]=sum[top-1]+(a[i]-'a'+1)*bin[top];//A的前缀hash if (top-m>=0){ if (hash1*bin[top-m+1] == (sum[top]-sum[top-m])){//匹配 top=top-m; } } } st[top+1]='\0'; printf("%s",st+1); return 0; }
- 1
信息
- ID
- 114
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 4
- 标签
- (无)
- 递交数
- 23
- 已通过
- 15
- 上传者