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

枚举最小表示的开头在哪个位置,然后求出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. LUA中将未分类数据分为测试集和训练集

    require 'torch' require 'image' local setting = {parent_root = '/home/pxu/image'} function list_chil ...

  2. 跟着百度学PHP[5]函数篇2-PHP中的特殊形式的函数

    目录...................................................... .00x1 可变函数 在PHP里面如果说将“函数名称”赋予字符串类型的变量.在调用这个 ...

  3. HTML5-本地存储与cookies

    一.H5的几种存储形式 1.本地存储(localstorage和sessionstorage) 存储形式:key-->value 过期策略:localstorage永久存储,不过期,除非手动删除 ...

  4. Memcache的增删改查

    Memcache是把数据存放到内存的一种缓存技术,为了提高访问的速度,memcache存储的数据一般是频繁.不太重要的数据,php使用memcache,需要两步: (1).php_memcache.d ...

  5. python scipy学习-曲线拟合

    根据某地每月的平均温度[17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18]拟合温度函数. import numpy as np import matplot ...

  6. C语言 活动安排问题

    有若干个活动,第i个开始时间和结束时间是[Si,fi),只有一个教室,活动之间不能交叠,求最多安排多少个活动? #include <stdio.h> #include <stdlib ...

  7. rsa密钥文件转化为tortoise认可的pak密钥文件

    原贴地址: http://www.vectorns.com/blog/technical-articles/1-tortoisesvn-over-ssh-on-windows-via-putty Ne ...

  8. 48. 二叉树两结点的最低共同父结点(3种变种情况)[Get lowest common ancestor of binary tree]

    [题目] 输入二叉树中的两个结点,输出这两个结点在数中最低的共同父结点. 二叉树的结点定义如下:  C++ Code  123456   struct BinaryTreeNode {     int ...

  9. 关于so文件cp覆盖导致调用者core的研究

    先说cp好mv/rm的区别: cp from to,则被覆盖文件 to的inode依旧不变(属性也不变),内容变为from的: mv from to,则to的inode变为from的,相应的,to的属 ...

  10. Android selecter背景选择器使用

    android:drawable这个属性是必须的,默认时的背景图片. android:state_pressed布尔值.true指当用户点击或者触摸该控件的状态.默认为false android:st ...