描述
给一个01串,找到他接受到的 01 串中所有重复出现次数大于 1 的子串。输出的顺序按对应的子串的字典序排列。
思路
搞出原串的n个后缀串。
然后用字母树或后缀数组。
应该叫后缀字母树。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define MAX 3001
struct node
{
int cnt;
node* next[2];
node(){cnt=1;next[0]=next[1]=NULL;}
}root[MAX*MAX],*etop;
void init()
{
etop=root;
etop++;
}
node *fr;
void add(int a)
{
if (!fr->next[a])
{
//etop->cnt=1;
fr->next[a]=etop;
etop++;
}
else
fr->next[a]->cnt++;
fr=fr->next[a];
}
int n;
char ch[MAX];
void dfs(node *p)
{
if(p->cnt>=2) printf("%d\n",p->cnt);
if(p->next[0]) dfs(p->next[0]);
if(p->next[1]) dfs(p->next[1]);
}
int main()
{
scanf("%d",&n);
scanf("%s",ch);
init();
for (int i=0;i<n;i++)
{
fr=root;
for (int j=i;j<n;j++)
{
add(ch[j]-'0');
}
}
dfs(root);
return 0;
}
感谢耒阳大视野教育培训机构: http://61.187.179.132/JudgeOnline/problem.php?id=2251