Acdream1116 Gao the string! (Hash binary or suffix array)

Source: Internet
Author: User

The problem sets a Fibonacci number. In the final analysis, it requires the total number of times all prefixes appear in all suffixes s [I... n-1. I thought for a long time at the beginning, and then I came to understand other people's solutions. For a suffix, s [I... n-1], all prefixes that match it must be the same as s [I + 1... n-1] s [I + 2... n-1]... s [n-1 .. n-1 we define a num [I] to indicate the suffix s [I... n-1] and the total length of the prefix is a common prefix, then num [I] + num [I + 1] + .. num [n-1] is the total number of times the prefix appears in the suffix I, so the problem is converted to s [I... n-1] The longest public prefix with the prefix can also be understood as s [0... n-1] And s [I... n-1] calculates the longest public prefix.

Find the longest public prefix of the suffix. One way is to create an array of suffixes, then create LCP, and then use rmq to ask about the theoretical complexity of nlogn. The template I set myself uses the quick rank, so the complexity of the suffix array is nlog ^ 2n, and then I will enjoy it.

After the failure, I can only use Hash + binary instead. After all, binary is still slow, so I am stuck with the time limit.

#pragma warning(disable:4996)#include <iostream>#include <cstring>#include <cstdio>#include <vector>#include <cmath>#include <string>#include <algorithm>using namespace std;#define maxn 110000#define ll long long#define mod 1000000007#define step 31/*int rk[maxn], sa[maxn], lcp[maxn];int tmp[maxn];int d[maxn + 50][25];*/int n;/*bool cmp_sa(int i, int j){if (rk[i] != rk[j]) return rk[i] < rk[j];else{int ri = i + k <= n ? rk[i + k] : -1;int rj = j + k <= n ? rk[j + k] : -1;return ri < rj;}}void construct_sa(char *s, int *sa){n = strlen(s);for (int i = 0; i <= n; i++){sa[i] = i;rk[i] = i < n ? s[i] : -1;}for (k = 1; k <= n; k <<= 1){sort(sa, sa + n + 1, cmp_sa);tmp[sa[0]] = 0;for (int i = 1; i <= n; i++){tmp[sa[i]] = tmp[sa[i - 1]] + (cmp_sa(sa[i - 1], sa[i]) ? 1 : 0);}for (int i = 0; i <= n; i++){rk[i] = tmp[i];}}}void construct_lcp(char *s, int *sa, int *lcp){n = strlen(s);for (int i = 0; i <= n; i++) rk[sa[i]] = i;int h = 0;lcp[0] = 0;for (int i = 0; i < n; i++){int j = sa[rk[i] - 1];for (h ? h-- : 0; i + h < n&&j + h < n&&s[i + h] == s[j + h]; h++);lcp[rk[i] - 1] = h;}}void construct_rmq(int *lcp, int sizen){for (int i = 0; i <= sizen; i++) d[i][0] = lcp[i];for (int j = 1; (1 << j) <= sizen; j++){for (int i = 0; (i + (1 << j) - 1) <= sizen; i++){d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);}}}int rmq_query(int l, int r){if (l > r) swap(l, r); r -= 1;int k = 0; int len = r - l + 1;while ((1 << (k + 1)) < len) k++;return min(d[l][k], d[r - (1 << k) + 1][k]);}*/char s[maxn];ll num[maxn];struct Matrix{ll a[2][2];Matrix(){ memset(a, 0, sizeof(a)); }}m;Matrix operator * (const Matrix &a,const Matrix &b){Matrix ret;for (int i = 0; i < 2; i++){for (int j = 0; j < 2; j++){for (int k = 0; k < 2; k++){ret.a[i][j] += (a.a[i][k] * b.a[k][j]) % mod;ret.a[i][j] %= mod;}}}return ret;}Matrix operator ^ (Matrix a,ll n){Matrix ret;for (int i = 0; i < 2; i++) ret.a[i][i] = 1;while (n){if (n & 1) ret = ret*a;n >>= 1;a = a*a;}return ret;}ll cal(ll n){m.a[0][0] = 0; m.a[0][1] = 1;m.a[1][0] = 1; m.a[1][1] = 1;m = m^n;return m.a[0][1];}ll seed[maxn];ll h[maxn];ll get(int l, int r){return ((h[r] - h[l - 1] * seed[r - l + 1]%mod) + mod) % mod;}int getlcp(int x){if (get(1, 1) != get(x, x)) return 0;int l = 1,r = n - x + 1;while (l <= r){int m = (l + r) >> 1;if (get(1, 1 + m - 1) == get(x, x + m - 1)) l = m+1;else r = m-1;}return r;}int main(){seed[0] = 1;for (int i = 1; i < maxn; i++){seed[i] = seed[i - 1] * step%mod;}while (~scanf("%s", s + 1)){n = strlen(s + 1); h[0] = 0;for (int i = 1; i <= n; i++){h[i] = (h[i - 1] * step + s[i]) % mod;}num[1] = n;for (int i = 2; i <= n; i++){num[i] = getlcp(i);}ll ans = 0;num[n + 1] = 0;for (int i = n; i >= 1; i--){num[i] += num[i + 1];ans += cal(num[i]);ans %= mod;}printf("%lld\n", ans);}return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.