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

枚举最小表示的开头在哪个位置,然后求出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. 再说表单验证,在Web Api中使用ModelState进行接口参数验证

    写在前面 上篇文章中说到了表单验证的问题,然后尝试了一下用扩展方法实现链式编程,评论区大家讨论的非常激烈也推荐了一些很强大的验证插件.其中一位园友提到了说可以使用MVC的ModelState,因为之前 ...

  2. 奇怪的bug(ant-design)

    ant-motion模板代码启动报错. 多了一层 import 会导致 less 编译的顺序发生变化,很奇怪的问题,还需要再深入看看.目前 ant-d.less 可以先改成这样来解决: + @impo ...

  3. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

  4. Android6.0权限组申请

    void checkPermission() { final List<String> permissionsList = new ArrayList<>(); if (Bui ...

  5. iOS sqlite数据库图像化查看

    问题描述:在xocde上用sqlite数据库的时候,因为没有图形化界面,有些时候很难看出自己设计的数据库是否有问题,比如我刚上手sqlite数据库设计id为自增长时,很自然的用了identify(1, ...

  6. [PHP][位转换积累]之异或运算的简单加密应用

    异或的符号是^.按位异或运算, 对等长二进制模式按位或二进制数的每一位执行逻辑按位异或操作. 操作的结果是如果某位不同则该位为1, 否则该位为0. xor运算的逆运算是它本身,也就是说两次异或同一个数 ...

  7. WPF直接用Window.Close直接关闭窗口导致不能完全退出的问题

    前几天我在CSDN扔了一个问题,基本描述一下:写了一段这样的代码,来实现获取Control的template,却发现一个这样的问题,就是当我打开了一个window以后,手动调用Close(),窗口的确 ...

  8. sh3.useradd 添加用户脚本

    1.写一个脚本: 添加10个用户user1到user10,密码同用户名,但要求只有用户不存在的情况下才能添加 #/bin/bash # ..};do if id user$i &> /d ...

  9. MFC序列化与反序列化

    #pragma once #include "afx.h" #include <vector> using std::vector; class HzyData : p ...

  10. 数据结构之列表-javascript实现

    学习数据结构的记录 列表是一种数据项构成的有限序列,即按照一定的线性顺序,排列而成的数据项的集合,在这种数据结构上进行的基本操作包括对元素的的查找,插入,和删除 列表的两种主要表现是数组和链表,栈和队 ...