WHU 583 Palindrome ( 回文自动机 && 本质不同的回文串的个数 )
题意 : 给你一个串、要你将其划分成两个串、使得左边的串的本质不同回文子串的个数是右边串的两倍、对于每一个这样子的划分、其对答案的贡献就是左边串的长度、现在要你找出所有这样子的划分、并将贡献乘起来、答案 mod 1e9+7
分析 :
从左到右跑一边回文自动机、对于每个前缀
能够得出其有多少个本质不同的回文子串
本质不同的回文子串的个数实际上就是自动机节点数 - 2
那么跑一遍前缀之后我们能得到所有可作为左边部分串的本质不同回文子串的个数
因为是回文串、所以我们倒着跑一遍、就同样能得到作为右边部分串的本质不同回文子串的个数
最后暴力检查一遍对于每一个位置是否有符合题意的合理划分、如果有就将左边部分长度累乘起来
#include<bits/stdc++.h> #define LL long long #define ULL unsigned long long #define scl(i) scanf("%lld", &i) #define scll(i, j) scanf("%lld %lld", &i, &j) #define sclll(i, j, k) scanf("%lld %lld %lld", &i, &j, &k) #define scllll(i, j, k, l) scanf("%lld %lld %lld %lld", &i, &j, &k, &l) #define scs(i) scanf("%s", i) #define sci(i) scanf("%d", &i) #define scd(i) scanf("%lf", &i) #define scIl(i) scanf("%I64d", &i) #define scii(i, j) scanf("%d %d", &i, &j) #define scdd(i, j) scanf("%lf %lf", &i, &j) #define scIll(i, j) scanf("%I64d %I64d", &i, &j) #define sciii(i, j, k) scanf("%d %d %d", &i, &j, &k) #define scddd(i, j, k) scanf("%lf %lf %lf", &i, &j, &k) #define scIlll(i, j, k) scanf("%I64d %I64d %I64d", &i, &j, &k) #define sciiii(i, j, k, l) scanf("%d %d %d %d", &i, &j, &k, &l) #define scdddd(i, j, k, l) scanf("%lf %lf %lf %lf", &i, &j, &k, &l) #define scIllll(i, j, k, l) scanf("%I64d %I64d %I64d %I64d", &i, &j, &k, &l) #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #define lowbit(i) (i & (-i)) #define mem(i, j) memset(i, j, sizeof(i)) #define fir first #define sec second #define VI vector<int> #define ins(i) insert(i) #define pb(i) push_back(i) #define pii pair<int, int> #define VL vector<long long> #define mk(i, j) make_pair(i, j) #define all(i) i.begin(), i.end() #define pll pair<long long, long long> #define _TIME 0 #define _INPUT 0 #define _OUTPUT 0 clock_t START, END; void __stTIME(); void __enTIME(); void __IOPUT(); using namespace std; struct Palindromic_Tree { static const int maxn = 400000; static const int Letter = 26 ; deque<int> text; int len[maxn];///len[i]表示节点i表示的回文串的长度 int cnt[maxn];///第 i 节点表示的回文串出现的次数、注意最后调用 getCnt 函数完成计算 int num[maxn];///表示以节点 i 代表的回文串端点为端点的不同回文串个数 int nxt[maxn][Letter];///nxt静态指针和字典树类似,指向的串为当前串两端加上同一个字符构成 int fail[maxn];///失配指针 int odd, even;///奇偶原始节点 int pre, suf; int totNode;///自动机所有节点的个数 LL totNum;///串中所有回文串数量 int newNode(int _len){ int ret = totNode++; len[ret] = _len; cnt[ret] = num[ret] = 0; fail[ret] = 0; memset(nxt[ret], 0, sizeof(nxt[ret])); return ret; } inline void init(){ text.clear(); totNode = 0; totNum = 0; even = newNode(0); odd = newNode(-1); fail[even] = fail[odd] = odd; pre = suf = even; } int encode(char c){ return c - 'a'; } template<typename FunT> int getFail(int ch, int cur, FunT getNext){ for(int i = getNext(len[cur]); i < 0 || i >= (int)text.size() || text[i] != ch; cur = fail[cur], i = getNext(len[cur])); return cur; } template<typename FunT> int Insert(int ch, int last, FunT getNext){ last = getFail(ch, last, getNext); if(!nxt[last][ch]){ int cur = newNode(len[last] + 2); fail[cur] = nxt[getFail(ch, fail[last], getNext)][ch]; nxt[last][ch] = cur; num[cur] = num[fail[cur]] + 1; } last = nxt[last][ch]; totNum += (LL)num[last]; cnt[last]++; return last; } int push_front(char c){ text.push_front(encode(c)); pre = Insert(encode(c), pre, [](int i)->int{ return i + 1; }); if(len[pre] == (int)text.size()) suf = pre; return totNode; } int push_back(char c){ text.push_back(encode(c)); suf = Insert(encode(c), suf, [this](int i)->int{ return this->text.size() - i - 2; }); if(len[suf] == (int)text.size()) pre = suf; return totNode; } int push_front(char *s){ int ret = 0; for(int i=0; s[i]; i++) ret = push_front(s[i]); return ret; } int push_back(char *s){ int ret = 0; for(int i=0; s[i]; i++) ret = push_back(s[i]); return ret; } inline void getCnt(){ for(int i=totNode-1; i>=0; i--) cnt[fail[i]] += cnt[i]; } }PAM1, PAM2; const int maxn = 4e5 + 10; const LL mod = 1e9 + 7; char str[maxn]; LL L[maxn], R[maxn]; int main(void){__stTIME();__IOPUT(); int T; sci(T); while(T--){ mem(L, 0); mem(R, 0); PAM1.init(); PAM2.init(); scs(str); int len = strlen(str); for(int i=0; i<len; i++){ PAM1.push_back(str[i]); L[i] = PAM1.totNode - 2; } for(int i=len-1; i>=0; i--){ PAM2.push_back(str[i]); R[i] = PAM2.totNode - 2; } LL ans = 0; for(int i=0; i<len; i++){ if(L[i] == 2 * R[i+1]){ if(ans == 0) ans = 1LL; ans = (1LL * (i+1) * ans) % mod; } } printf("%lld\n", ans); } __enTIME();return 0;} void __stTIME() { #if _TIME START = clock(); #endif } void __enTIME() { #if _TIME END = clock(); cerr<<"execute time = "<<(double)(END-START)/CLOCKS_PER_SEC<<endl; #endif } void __IOPUT() { #if _INPUT freopen("in.txt", "r", stdin); #endif #if _OUTPUT freopen("out.txt", "w", stdout); #endif }