#include<bits/stdc++.h>
#include <unordered_map>
using namespace std;
template<class...Args>
void debug(Args... args) {//Parameter pack
auto tmp = { (cout << args << ' ', 0)... };
cout << "\n";
}
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>pll;
typedef pair<int, int>pii;
const ll N = 5e5 + 5;
const ll INF = 0x7fffffff;
vector<int> get_z(string s) {//求整个串s与s[i]开头的后缀的最长公共前后缀的长度
int n = (int)s.length();
vector<int> z(n);
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r && z[i - l] < r - i + 1) {
z[i] = z[i - l];
}
else {
z[i] = max(0, r - i + 1);
while (i + z[i] < n && s[z[i]] == s[i + z[i]]) ++z[i];
}
if (i + z[i] - 1 > r) l = i, r = i + z[i] - 1;
}
return z;
}
vector<int> get_next(string s) {//求以s[i]结束的子串s中最长公共前后缀的长度
int n = (int)s.length();
vector<int> _next(n);
for (int i = 1; i < n; i++) {
int j = _next[i - 1];
while (j > 0 && s[i] != s[j]) j = _next[j - 1];
if (s[i] == s[j]) j++;
_next[i] = j;
}
return _next;
}
//https://acm.hdu.edu.cn/showproblem.php?pid=3336
vector<int> get_per(string s) {//求给定字符串每个前缀在整个字符串出现的次数
int n = (int)s.length();
vector<int> _next = get_next(s);
vector<int> ans(n + 1);
for (int i = 0; i < n; i++) ans[_next[i]]++;
for (int i = n - 1; i > 0; i--) ans[_next[i - 1]] += ans[i];
for (int i = 0; i <= n; i++) ans[i]++;
return ans;
}
//
int count_unique_substrings(string const& s) {//给定字符串,仅由小写英文字母组成,查找该字符串中不同子串的数量
int n = s.size();
const int b = 31;
const int m = 1e9 + 9;
vector<long long> b_pow(n);
b_pow[0] = 1;
for (int i = 1; i < n; i++) b_pow[i] = (b_pow[i - 1] * b) % m;
vector<long long> h(n + 1, 0);
for (int i = 0; i < n; i++)
h[i + 1] = (h[i] + (s[i] - 'a' + 1) * b_pow[i]) % m;
int cnt = 0;
for (int l = 1; l <= n; l++) {
set<long long> hs;
for (int i = 0; i <= n - l; i++) {
long long cur_h = (h[i + l] + m - h[i]) % m;
cur_h = (cur_h * b_pow[n - i - 1]) % m;
hs.insert(cur_h);
}
cnt += hs.size();
}
return cnt;
}
//https://www.luogu.com.cn/problem/P6114
vector<string> duval(string const& s) {//Lyndon 分解方案
int n = s.size(), i = 0;
vector<string> factorization;
ll ans = 0;
while (i < n) {
int j = i + 1, k = i;
while (j < n && s[k] <= s[j]) {
if (s[k] < s[j])
k = i;
else
k++;
j++;
}
while (i <= k) {
factorization.push_back(s.substr(i, j - k));
i += j - k;
ans = ans ^ i;
}
}
cout << ans << "\n";
return factorization;
}
string min_cyclic_string(string s) {//最小表示法
s += s;
int n = s.size();
int i = 0, ans = 0;
while (i < n / 2) {
ans = i;
int j = i + 1, k = i;
while (j < n && s[k] <= s[j]) {
if (s[k] < s[j])
k = i;
else
k++;
j++;
}
while (i <= k) i += j - k;
}
return s.substr(ans, n / 2);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
/*string s = "abacaba";
vector<int> Z = get_z(s);
for (auto i : Z)cout << i << " ";
cout << "\n";
vector<int> _next = get_next(s);
for (auto i : _next)cout << i << " ";
cout << "\n";
vector<int> per = get_per("abab");
for (auto i : per)cout << i << " ";
cout << "\n";*/
/*string s;
int t;
cin >> t;
while (t--) {
char t;
cin >> t;
s.push_back(t);
}
s=min_cyclic_string(s);
for (auto i : s)cout << s << " ";
cout << "\n";*/
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
cout << count_unique_substrings(s) << "\n";
}
return 0;
}
字符串的一些模板
最新推荐文章于 2022-11-27 11:21:09 发布