# HDU 5558 Alice's Classified Message (后缀自动机水题) 2015年合肥区域赛G题

(貌似现场赛这个题数据范围被缩小了, 于是成了一个全场都能过的题....)

Result  :  Accepted     Memory  :  42476 KB     Time  :  592 ms

/*
* Author: Gatevin
* Created Time:  2015/11/20 10:23:20
* File Name: Sakura_Chiyo.cpp
*/
#include<iostream>
#include<sstream>
#include<fstream>
#include<vector>
#include<list>
#include<deque>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<cmath>
#include<ctime>
#include<iomanip>
using namespace std;
const double eps(1e-8);
typedef long long lint;

#define maxn (100010*2)
#define maxm 100010

struct Suffix_Automation
{
struct State
{
State *par;
State *go[26];
int val;
int mi;
int cnt;
int right;
int leftmost;
void init(int _val = 0)
{
par = 0, val = _val, cnt = 0, mi = 0, right = 0, leftmost = 1e9;
memset(go, 0, sizeof(go));
}
};
State *root, *last, *cur;
State nodePool[maxn];
State* newState(int val = 0)
{
cur->init(val);
return cur++;
}
void initSAM()
{
cur = nodePool;
root = newState();
last = root;
}
{
State *p = last;
State *np = newState(p->val + 1);
np->right = 1;
while(p && p->go[w] == 0)
{
p->go[w] = np;
p = p->par;
}
if(p == 0)
{
np->par = root;
}
else
{
State *q = p->go[w];
if(p->val + 1 == q->val)
{
np->par = q;
}
else
{
State *nq = newState(p->val + 1);
memcpy(nq->go, q->go, sizeof(q->go));
nq->leftmost = q->leftmost;
nq->par = q->par;
q->par = nq;
np->par = nq;
while(p && p->go[w] == q)
{
p->go[w] = nq;
p = p->par;
}
}
}
last = np;
}
void solve(char *s)
{
initSAM();
int n = strlen(s);
int now = 0;
while(now != n)
{
State* pos = root;
int deep = 0;
while(now < n && pos->go[s[now] - 'a'] != 0)
{
pos = pos->go[s[now] - 'a'];
deep++;
extend(s[now] - 'a', now);
now++;
}
if(deep == 0)
{
extend(s[now] - 'a', now);
printf("-1 %d\n", s[now++]);
}
else printf("%d %d\n", deep, pos->leftmost - deep + 1);
}
}
};

Suffix_Automation sam;
char s[maxm];

int main()
{
int T;
scanf("%d", &T);
for(int cas = 1; cas <= T; cas++)
{
printf("Case #%d:\n", cas);
scanf("%s", s);
sam.solve(s);
}
return 0;
}
/*

2
aaaaaa
aaaaabbbbbaaabbc

*/

• 本文已收录于以下专栏：

举报原因： 您举报文章：HDU 5558 Alice's Classified Message (后缀自动机水题) 2015年合肥区域赛G题 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)