题意:
给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,
圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比
如aaab变成baaa,abaa,aaba再进行匹配。
题解:
如何求出所有的循环串出现的次数呢?
先将S串放入后缀自动机
把查询串扩大一倍,然后在后缀自动机上去匹配,
只要匹配长度大于子串长度小于2倍子串长度的,必然对应这一种循环串
记录一下
// 每个节点子串出现的次数
统计答案即可
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <set> 2 #include <map> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <cstdio> 8 #include <string> 9 #include <vector> 10 #include <cstring> 11 #include <iostream> 12 #include <algorithm> 13 #include <unordered_map> 14 15 #define pi acos(-1.0) 16 #define eps 1e-9 17 #define fi first 18 #define se second 19 #define rtl rt<<1 20 #define rtr rt<<1|1 21 #define bug printf("******\n") 22 #define mem(a, b) memset(a,b,sizeof(a)) 23 #define name2str(x) #x 24 #define fuck(x) cout<<#x" = "<<x<<endl 25 #define sfi(a) scanf("%d", &a) 26 #define sffi(a, b) scanf("%d %d", &a, &b) 27 #define sfffi(a, b, c) scanf("%d %d %d", &a, &b, &c) 28 #define sffffi(a, b, c, d) scanf("%d %d %d %d", &a, &b, &c, &d) 29 #define sfL(a) scanf("%lld", &a) 30 #define sffL(a, b) scanf("%lld %lld", &a, &b) 31 #define sfffL(a, b, c) scanf("%lld %lld %lld", &a, &b, &c) 32 #define sffffL(a, b, c, d) scanf("%lld %lld %lld %lld", &a, &b, &c, &d) 33 #define sfs(a) scanf("%s", a) 34 #define sffs(a, b) scanf("%s %s", a, b) 35 #define sfffs(a, b, c) scanf("%s %s %s", a, b, c) 36 #define sffffs(a, b, c, d) scanf("%s %s %s %s", a, b,c, d) 37 #define FIN freopen("../in.txt","r",stdin) 38 #define gcd(a, b) __gcd(a,b) 39 #define lowbit(x) x&-x 40 #define IO iOS::sync_with_stdio(false) 41 42 43 using namespace std; 44 typedef long long LL; 45 typedef unsigned long long ULL; 46 const ULL seed = 13331; 47 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL; 48 const int maxm = 8e6 + 10; 49 const int INF = 0x3f3f3f3f; 50 const int mod = 1e9 + 7; 51 const int maxn = 250007; 52 char s[maxn]; 53 int Q; 54 55 struct Suffix_Automaton { 56 int last, tot, nxt[maxn << 1][26], fail[maxn << 1];//last是未加入此字符前最长的前缀(整个串)所属的节点的编号 57 int len[maxn << 1];// 最长子串的长度 (该节点子串数量 = len[x] - len[fa[x]]) 58 int sa[maxn << 1], c[maxn << 1]; 59 int sz[maxn << 1];// 被后缀链接的个数,方便求节点字符串的个数 60 LL num[maxn << 1];// 该状态子串的数量 61 LL maxx[maxn << 1];// 长度为x的子串出现次数最多的子串的数目 62 LL sum[maxn << 1];// 该节点后面所形成的自字符串的总数 63 LL subnum, sublen;// subnum表示不同字符串数目,sublen表示不同字符串总长度 64 int X[maxn << 1], Y[maxn << 1]; // Y表示排名为x的节点,X表示该长度前面还有多少个 65 int minn[maxn << 1], mx[maxn << 1];//minn[i]表示多个串在后缀自动机i节点最长公共子串,mx[i]表示单个串的最长公共子串 66 void init() { 67 tot = last = 1; 68 fail[1] = len[1] = 0; 69 for (int i = 0; i < 26; i++) nxt[1][i] = 0; 70 } 71 72 void extend(int c) { 73 int u = ++tot, v = last; 74 len[u] = len[v] + 1; 75 num[u] = 1; 76 for (; v && !nxt[v][c]; v = fail[v]) nxt[v][c] = u; 77 if (!v) fail[u] = 1, sz[1]++; 78 else if (len[nxt[v][c]] == len[v] + 1) fail[u] = nxt[v][c], sz[nxt[v][c]]++; 79 else { 80 int now = ++tot, cur = nxt[v][c]; 81 len[now] = len[v] + 1; 82 memcpy(nxt[now], nxt[cur], sizeof(nxt[cur])); 83 fail[now] = fail[cur]; 84 fail[cur] = fail[u] = now; 85 for (; v && nxt[v][c] == cur; v = fail[v]) nxt[v][c] = now; 86 sz[now] += 2; 87 } 88 last = u; 89 //return len[last] - len[fail[last]];//多添加一个子串所产生不同子串的个数 90 } 91 92 void get_num() {// 每个节点子串出现的次数 93 for (int i = 1; i <= tot; i++) X[len[i]]++; 94 for (int i = 1; i <= tot; i++) X[i] += X[i - 1]; 95 for (int i = 1; i <= tot; i++) Y[X[len[i]]--] = i; 96 for (int i = tot; i >= 1; i--) num[fail[Y[i]]] += num[Y[i]]; 97 } 98 99 void get_maxx(int n) {// 长度为x的子串出现次数最多的子串的数目 100 get_num(); 101 for (int i = 1; i <= tot; i++) maxx[len[i]] = max(maxx[len[i]], num[i]); 102 } 103 104 void get_sum() {// 该节点后面所形成的自字符串的总数 105 get_num(); 106 for (int i = tot; i >= 1; i--) { 107 sum[Y[i]] = 1; 108 for (int j = 0; j <= 25; j++) 109 sum[Y[i]] += sum[nxt[Y[i]][j]]; 110 } 111 } 112 113 void get_subnum() {//本质不同的子串的个数 114 subnum = 0; 115 for (int i = 1; i <= tot; i++) subnum += len[i] - len[fail[i]]; 116 } 117 118 void get_sublen() {//本质不同的子串的总长度 119 sublen = 0; 120 for (int i = 1; i <= tot; i++) sublen += 1LL * (len[i] + len[fail[i]] + 1) * (len[i] - len[fail[i]]) / 2; 121 } 122 123 void get_sa() { //获取sa数组 124 for (int i = 1; i <= tot; i++) c[len[i]]++; 125 for (int i = 1; i <= tot; i++) c[i] += c[i - 1]; 126 for (int i = tot; i >= 1; i--) sa[c[len[i]]--] = i; 127 } 128 129 void match(char s[]) {//多个串的最长公共子串 130 mem(mx, 0); 131 int n = strlen(s), p = 1, maxlen = 0; 132 for (int i = 0; i < n; i++) { 133 int c = s[i] - 'a'; 134 if (nxt[p][c]) p = nxt[p][c], maxlen++; 135 else { 136 for (; p && !nxt[p][c]; p = fail[p]); 137 if (!p) p = 1, maxlen = 0; 138 else maxlen = len[p] + 1, p = nxt[p][c]; 139 } 140 mx[p] = max(mx[p], maxlen); 141 } 142 for (int i = tot; i; i--) 143 mx[fail[i]] = max(mx[fail[i]], min(len[fail[i]], mx[i])); 144 for (int i = tot; i; i--) 145 if (minn[i] == -1 || minn[i] > maxx[i]) minn[i] = mx[i]; 146 } 147 148 void get_kth(int k) {//求出字典序第K的子串 149 int pos = 1, cnt; 150 string s = ""; 151 while (k) { 152 for (int i = 0; i <= 25; i++) { 153 if (nxt[pos][i] && k) { 154 cnt = nxt[pos][i]; 155 if (sum[cnt] < k) k -= sum[cnt]; 156 else { 157 k--; 158 pos = cnt; 159 s += (char) (i + 'a'); 160 break; 161 } 162 } 163 } 164 } 165 cout << s << endl; 166 } 167 168 int vis[maxn << 1]; 169 170 void solve(int lenlen, int id) { 171 int maxlen = 0, p = 1, ans = 0; 172 for (int i = 1; i <= lenlen * 2; i++) { 173 int c = s[i] - 'a'; 174 if (nxt[p][c]) maxlen++, p = nxt[p][c]; 175 else { 176 for (; p && !nxt[p][c]; p = fail[p]); 177 if (p == 0) p = 1, maxlen = 0; 178 else maxlen = len[p] + 1, p = nxt[p][c]; 179 } 180 if (i >= lenlen && maxlen >= lenlen) { 181 int temp = p; 182 while (temp && !(len[fail[temp]] + 1 <= lenlen && len[temp] >= lenlen))temp = fail[temp]; 183 if (!temp) temp = 1; 184 if (vis[temp] != id) vis[temp] = id, ans += num[temp]; 185 } 186 } 187 printf("%d\n", ans); 188 } 189 } sam; 190 191 int main() { 192 #ifndef ONLINE_JUDGE 193 FIN; 194 #endif 195 sam.init(); 196 sfs(s + 1); 197 int n = strlen(s + 1); 198 for (int i = 1; i <= n; i++) sam.extend((s[i] - 'a')); 199 sam.get_num(); 200 sfi(Q); 201 for (int i = 1; i <= Q; i++) { 202 sfs(s + 1); 203 int len = strlen(s + 1); 204 for (int i = 1; i <= len; i++) s[i + len] = s[i]; 205 s[2 * len + 1] = '\0'; 206 //fuck(s + 1); 207 sam.solve(len, i); 208 } 209 #ifndef ONLINE_JUDGE 210 cout << "Totle Time : " << (double) clock() / CLOCKS_PER_SEC << "s" << endl; 211 #endif 212 return 0; 213 }