只要求出两个字符串的最小表示,然后就可以判断是否循环同构。

枚举最小表示的开头在哪个位置,然后求出Hash值,如果两个串的Hash值集合有交,那么说明循环同构。

因为串经过压缩,原串的长度很大,不能直接枚举开头。

考虑当开头在某个串$A^k$里某个位置时的性质:

假设$A^k$全在开头,现在考虑挪动一个$A$到结尾。

那么如果挪动之后字典序更小了,那么再挪动一个$A$到结尾,比较条件不变。

因此一旦挪动一个$A$之后字典序变小,那么最小表示一定是将$k-1$个$A$全部挪到结尾。

所以对于一个压缩串$A^k$,只需要在其第一次重复和最后一次重复的部分枚举开头即可。

对于这两部分,Hash值可以直接递推计算,对于中间$k-2$次移动,可以用矩阵快速幂计算。

时间复杂度$O(tk|S|\log|S|)$。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i) for(int i=0;i<2;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>PI;
const int N=30010,S=233,P[2]={1000000007,1000000009};
inline bool check(char x){return x>='a'&&x<='z';}
struct String{
char s[N];ll tot;
int cnt,n,len[N],w[N],st[N],en[N];
void read(){
static char a[N];
scanf("%s",a);
int l=strlen(a),i,j,k;
cnt=n=tot=0;
for(i=0;i<l;){
if(check(a[i])){
st[++n]=cnt+1;
for(j=i;j<l&&check(a[j]);j++);
for(k=i;k<j;k++)s[++cnt]=a[k];
en[n]=cnt;
len[n]=en[n]-st[n]+1;
w[n]=1;
tot+=len[n]*w[n];
i=j;
}else{
st[++n]=cnt+1;
for(j=++i;j<l&&check(a[j]);j++);
for(k=i;k<j;k++)s[++cnt]=a[k];
en[n]=cnt;
len[n]=en[n]-st[n]+1;
w[n]=0;
for(j++;j<l&&a[j]>='0'&&a[j]<='9';j++)w[n]=w[n]*10+a[j]-'0';
tot+=len[n]*w[n];
i=j;
}
}
}
void write(){
int i,j;
printf("%d %lld\n",n,tot);
for(i=1;i<=n;i++){
for(j=st[i];j<=en[i];j++)putchar(s[j]);
printf(" %d %d\n",len[i],w[i]);
}
}
}A,B;
namespace Hash{
char s[N];ll tot;
int cnt,n,i,j,k,x,y,len[N],w[N],st[N],en[N],f[N][2],g[2],mo;
struct mat{
int v[2][2];
mat(){}
mat operator*(const mat&b){
mat c;
rep(i)rep(j)c.v[i][j]=0;
rep(i)rep(j)rep(k)c.v[i][j]=(1LL*v[i][k]*b.v[k][j]+c.v[i][j])%mo;
return c;
}
};
inline int pow(int a,ll b,int P){int t=1;for(;b;b>>=1LL,a=1LL*a*a%P)if(b&1LL)t=1LL*t*a%P;return t;}
inline int cal(int f,int l,int w,int P){
mo=P;
mat A,B;
rep(i)rep(j)A.v[i][j]=B.v[i][j]=0;
B.v[1][0]=f;
A.v[0][0]=A.v[0][1]=1,A.v[1][1]=pow(S,l,P);
for(;w;w>>=1,A=A*A)if(w&1)B=A*B;
return B.v[0][0];
}
void solve(PI*v,int&cv,const String&p,ll L){
cv=cnt=n=0,tot=L;
for(i=1;i<=p.n;i++){
x=min(L/p.len[i],1LL*p.w[i]);
if(x){
st[++n]=cnt+1;
for(j=p.st[i];j<=p.en[i];j++)s[++cnt]=p.s[j];
en[n]=cnt;
len[n]=en[n]-st[n]+1;
w[n]=x;
}
L-=x*p.len[i];
if(!L)break;
if(L<p.len[i]&&x<p.w[i]){
st[++n]=cnt+1;
for(j=p.st[i];j<p.st[i]+L;j++)s[++cnt]=p.s[j];
en[n]=cnt;
len[n]=L;
w[n]=1;
break;
}
}
for(i=0;i<2;i++)g[i]=0;
for(i=1;i<=n;i++){
for(j=0;j<2;j++)f[i][j]=0;
for(j=st[i];j<=en[i];j++)for(k=0;k<2;k++)f[i][k]=(1LL*f[i][k]*S+s[j])%P[k];
for(j=0;j<2;j++)g[j]=(1LL*g[j]*pow(S,len[i]*w[i],P[j])+cal(f[i][j],len[i],w[i],P[j]))%P[j];
}
for(i=1;i<=n;i++){
for(j=st[i];j<=en[i];j++){
v[++cv]=PI(g[0],g[1]);
for(k=0;k<2;k++){
g[k]=(g[k]-1LL*s[j]*pow(S,tot-1,P[k])%P[k]+P[k])%P[k];
g[k]=(1LL*g[k]*S+s[j])%P[k];
}
}
if(w[i]==1)continue;
for(j=0;j<2;j++){
x=len[i]*(w[i]-2);
y=cal(f[i][j],len[i],w[i]-2,P[j]);
g[j]=(g[j]-1LL*y*pow(S,tot-x,P[j])%P[j]+P[j])%P[j];
g[j]=(1LL*g[j]*pow(S,x,P[j])+y)%P[j];
}
for(j=st[i];j<=en[i];j++){
v[++cv]=PI(g[0],g[1]);
for(k=0;k<2;k++){
g[k]=(g[k]-1LL*s[j]*pow(S,tot-1,P[k])%P[k]+P[k])%P[k];
g[k]=(1LL*g[k]*S+s[j])%P[k];
}
}
}
}
}
int T,C,k,o,ans,ca,cb,i,j;PI a[N],b[N];
int main(){
scanf("%d",&T);
for(C=1;C<=T;C++){
A.read();
scanf("%d",&k);ans=0;
for(o=1;o<=k;o++){
B.read();
Hash::solve(a,ca,A,B.tot);
Hash::solve(b,cb,B,B.tot);
sort(a+1,a+ca+1),sort(b+1,b+cb+1);
for(i=j=1;i<=ca&&j<=cb;){
if(a[i]<b[j])i++;
else if(a[i]>b[j])j++;
else{
ans+=o*o;
break;
}
}
}
printf("Case #%d: %d\n",C,ans);
}
return 0;
}

  

HDU5509 : Pattern String的更多相关文章

  1. (String). Word Pattern

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  2. A Simple C++ Template Class that Matches a String to a Wildcard Pattern

    A recently implemented enhanced wildcard string matcher, features of which including, Supporting wil ...

  3. java常用类与包装类--常用类正则表达式 String正则方法+Matcher+Pattern

    0.java中的正则 java 中的正则总共涉及三个类(或者说1个String类和一个regex包) java.lang.String java.util. Matcher java.util.Pat ...

  4. int preg_match( string pattern

    preg_match -- 进行正则表达式匹配.并且只匹配一次,注意与preg_match_all区别. int preg_match( string pattern, string subject ...

  5. [LeetCode] Word Pattern II 词语模式之二

    Given a pattern and a string str, find if str follows the same pattern. Here follow means a full mat ...

  6. [LeetCode] Word Pattern 词语模式

    Given a pattern and a string str, find if str follows the same pattern. Examples: pattern = "ab ...

  7. LeetCode 290 Word Pattern

    Problem: Given a pattern and a string str, find if str follows the same pattern. Here follow means a ...

  8. leetcode(一)Word Pattern

    题目描述: Given a pattern and a string str, find if str follows the same pattern. Here follow means a fu ...

  9. Lua 之string库

    标准string库 基础字符串函数 string.len(s) 返回一个字符串的长度,例如 string.rep(s, n) 返回一个新的字符串,该字符串是参数s重复n次得到的结果,例如 )) -- ...

随机推荐

  1. 国内github访问慢的解决方法

    本文是windows处理方法,macos方法也差不多. 一般Github的访问有两部分:主站的访问和二级域名的资源加载(比如样式文件等) 一般Github加载缓慢,主要是 assets-cdn.git ...

  2. python调用其他程序或脚本方法(转)

    python运行(调用)其他程序或脚本 在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地 ...

  3. Jquery制作--焦点图淡出淡入

    之前写了一个焦点图左右轮播的,感觉淡出淡入用得也比较多,就干脆一起放上来啦.这个容器用了百分比宽度,图片始终保持居中处理,定宽或者自适应宽度都是可以的. 兼容到IE6+以上浏览器,有淡出淡入速度和切换 ...

  4. win7系统下python安装numpy,matplotlib,scipy和scikit-learn

    1.安装numpy,matplotlib,scipy和scikit-learn win7系统下直接采用pip或者下载源文件进行安装numpy,matplotlib,scipy时会遇到各种问题,这是因为 ...

  5. JS 初级(三)接上

    传送门 http://www.cnblogs.com/Sabo-dudu/p/5788197.html 现阶段我就了解了这么多,在以后的学习中,我会不断的更新,如果有什么不同的见解可以一块学习,谁有更 ...

  6. sublime text快捷键

    Ctrl+Shift+V:粘贴并格式化Ctrl+D:选择单词,重复可增加选择下一个相同的单词Ctrl+L:选择行,重复可依次增加选择下一行Ctrl+M:跳转到对应括号Ctrl+K+B:开关侧栏Ctrl ...

  7. 已安装php 编译安装 gd库拓展模块

    参考资料:http://wenku.baidu.com/link?url=EgXFShYxeJOZSYNQ_7RCBC-6X8OcRRCqVm4qCv49uBk57d6vLBoUpfYdQ-KqJRs ...

  8. erlang 故障排查工具

    系统级别perf top, dstat -tam, vtune 都能很好分析beam 瓶颈,本文主要erlang 级别排查: 1. 反编译 确认线上运行代码是否正确,reltools没掌握好,升级偶尔 ...

  9. 让“是男人就下到100层”在Android平台上跑起来

    原工程:https://github.com/jeekun/DownFloors 移植后的代码:HelloCpp.zip 移植后的APK:HelloCpp.apk 说明:(cocos2d-x版本是“ ...

  10. 纯html页面之间传参

    //页面引入//传参方法,可解析url参数 (function($){ $.getUrlParam = function(name) { var reg = new RegExp("(^|& ...