俗話說的好,模板題怎麼能用模板水過去呢
我們考慮用建出\(ht\)陣列,然後用單調棧求出每個\(ht\)最多能向左向右延伸多遠(vi.[ahoi2013]差異),然後直接一邊掃過求\(\max\)即可。
複雜度\(o(n)\),假如你用dc3的話。但是用倍增實際跑起來也真的超快!!!
**:
#includeusing namespace std;
typedef long long ll;
const int n=1001000;
int n,m;
int x[n],y[n],sa[n],ht[n],rk[n],buc[n];
char s[n];
bool mat(int a,int b,int k)
for(int i=0;iht[i])r[stk[tp]]=i,tp--;
l[i]=stk[tp],stk[++tp]=i;
} while(tp)r[stk[tp--]]=n;
for(int i=1;iprintf("%lld\n",res);
return 0;
}
模板 字尾自動機 SAM SAM解法
雖說是模板但也不是純粹板子!sam應用1 求乙個子串的出現次數 相當於字串匹配 因為同乙個 text 類中所有東西的結束集合都是相同的,故它們在母串中的出現次數也是相同的。因此,在該類中就應該貪心地選擇最長的串,也即 text 那麼我們如何求出乙個 text 類中所有東西的出現次數呢?考慮paren...
字尾自動機模板
include include define maxc 28 using namespace std const int maxn 1e6 5 const int mod 1e9 7 typedef long long ll int len maxn 2 最長子串的長度 該節點字串數量 len x ...
模板 字尾自動機
求子串的出現次數。用類似拓撲排序的思想,從沒有出度的節點開始把他的cnt加在他的字尾連線上。include using namespace std typedef long long ll struct node void copynewnode const int l,const node n c...