这题用SA做的很伤心,用SAM比用SA做要简单,这类串的计数类的题目属于比较麻烦的题,需要记录的信息较多,首先要把每个状态的min和|right|都预处理出来,还需要记录四个额外的信息,见代码注释吧。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <ctime>
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef LL TY;
const int MAXN(100010);
const int MAXM(50010);
const int MAXE(100010);
const int MAXK(6);
const int HSIZE(131313);
const int SIGMA_SIZE(52);
const int MAXH(19);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(10000000);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
template<typename T> void checkmax(T &a, T b){if(b > a) a = b;}
template<typename T> void checkmin(T &a, T b){if(b < a) a = b;}
template<typename T> T ABS(const T &a){return a < 0? -a: a;}
char str[MAXN];
int K;
int cnt[MAXN];
int buc[MAXN << 1];
int hash[128];
struct SAM
{
struct NODE
{
int len, mi, right;
LL cnt;
NODE *f, *ch[SIGMA_SIZE];
};
NODE *root, *last;
NODE pool[MAXN << 1];
int size;
void init()
{
root = last = pool;
root->f = 0;
root->len = 0;
root->cnt = 0;
root->len = root->mi = root->right = 0;
memset(root->ch, 0, sizeof(root->ch));
size = 1;
}
NODE *newnode(int tl)
{
pool[size].len = tl;
pool[size].cnt = 0;
memset(pool[size].ch, 0, sizeof(pool[size].ch));
return pool+size++;
}
void extend(int id)
{
NODE *p = last, *np = newnode(last->len+1);
np->right = 1;
last = np;
while(p && p->ch[id] == 0)
p->ch[id] = np, p = p->f;
if(p == 0)
np->f = root;
else
{
NODE *q = p->ch[id];
if(p->len+1 == q->len)
np->f = q;
else
{
NODE *nq = newnode(p->len+1);
memcpy(nq->ch, q->ch, sizeof(nq->ch));
nq->right = 0;
nq->f = q->f;
q->f = np->f = nq;
while(p && p->ch[id] == q)
p->ch[id] = nq, p = p->f;
}
}
}
void tupo()
{
int tl = last->len;
memset(cnt, 0, sizeof(cnt[0])*(tl+1));
for(int i = 0; i < size; ++i) ++cnt[pool[i].len];
for(int i = 1; i <= tl; ++i) cnt[i] += cnt[i-1];
for(int i = 0; i < size; ++i) buc[--cnt[pool[i].len]] = i;
}
void getBack()
{
NODE *p;
for(int i = size-1; i > 0; --i)
{
p = pool+buc[i];
p->f->right += p->right;
p->mi = p->f->len+1;
}
}
void solve()
{
tupo();
getBack();
LL ans = 0;
NODE *p = root;
int tl = 0;
for(char *sp = str; *sp; ++sp)
{
int id = hash[*sp];
if(p->ch[id])
{
p = p->ch[id];
++tl;
}
else
{
while(p && p->ch[id] == 0) p = p->f;
if(p)
{
tl = p->len+1;
p = p->ch[id];
}
else
{
p = root;
tl = 0;
}
}
if(tl >= K)
{
ans += (tl-max(K, p->mi)+1)*p->right;
++p->cnt;
}
}
for(int i = size-1; pool[buc[i]].len >= K; --i)
{
p = pool+buc[i];
if(p->f->len >= K)
ans += p->cnt*(p->f->len-max(K, p->f->mi)+1)*p->f->right;
p->f->cnt += p->cnt;
}
printf("%I64d\n", ans);
}
}sam;
int main()
{
int cnt = 0;
for(int i = 'a'; i <= 'z'; ++i)
hash[i] = cnt++;
for(int i = 'A'; i <= 'Z'; ++i)
hash[i] = cnt++;
while(scanf("%d", &K), K)
{
scanf("%s", str);
sam.init();
for(char *sp = str; *sp; ++sp)
sam.extend(hash[*sp]);
scanf("%s", str);
sam.solve();
}
return 0;
}