题意:

  T组数据,每组数据给出一个字符串,求这个字符串的最小表示发(只要求输出起始位置坐标)


  SAM入门题(检测板子是否正确)。

  将字符串S加倍丢进SAM中,然后走字符串长度次,每次贪心的沿最小的边走,然后答案就是:sam.e[po].len-len+1


 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 10010
#define llg long long
#define SIZE 26
#define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
llg n,m,T; char s[maxn]; struct SAM
{
struct
{
llg len,f/*parent边*/,ch[SIZE];
void init()
{
len=,f=-;
memset(ch,0xff,sizeof(ch));
}
}e[maxn<<];
llg idx,last; void init() {idx=last=; e[idx++].init();} int newnode() {e[idx].init(); return idx++;} void add(llg c)
{
int end=newnode(),tmp=last;
e[end].len=e[last].len+;
for (;tmp!=- && e[tmp].ch[c]==-;tmp=e[tmp].f){e[tmp].ch[c]=end;}//跳parent tree的边,找到第一个具有c出边的点并停下,对于没有的点连出一条边权是c的边指向当前点(end)。
if (tmp==-) e[end].f=;//如果没有任何一个点有权值为c的出边,则说明了相应字符c是第一次出现在自动机中。
else
{
llg nxt=e[tmp].ch[c];
if (e[tmp].len+==e[nxt].len) e[end].f=nxt;//如果找到的第一个具有出边c的点的出边c左指向点的len=lastlen+1,则直接把新建点的parent边连向nxt点。
else
{
llg np=newnode();
e[np]=e[nxt];
e[np].len=e[tmp].len+;//新建点np
e[nxt].f=e[end].f=np;
for (;tmp!=- && e[tmp].ch[c]==nxt;tmp=e[tmp].f) {e[tmp].ch[c]=np;}//沿parent边往祖先走把所有原本连向nxt的带边权c的边改为连向np
}//如果不满足MAX(s)+1=MAX(s[tmp][c])则新建点
}
last=end;
}
}sam; int main()
{
yyj("SAM");
cin>>T;
while (T--)
{
sam.init();
scanf("%s",s);
llg len=strlen(s);
for (llg i=;i<len;i++) sam.add(s[i]-'a');
for (llg i=;i<len;i++) sam.add(s[i]-'a');
llg po=;
for (llg i=;i<len;i++)
for (llg j=;j<;j++)
if (sam.e[po].ch[j]!=-)
{
po=sam.e[po].ch[j];
break;
}
printf("%lld\n",sam.e[po].len-len+);
}
return ;
}

最新文章

  1. 用ip来获得用户所在地区信息
  2. java 22 - 11 多线程之模拟电影院售票口售票
  3. 【项目经验】--EasyUI DataGrid之右键菜单
  4. JMeter学习-002-JMeter环境配置
  5. C#通过反射获取上层调用方法信息
  6. 【Android】事件总线(解耦组件) EventBus 详解
  7. paip.自定义java 泛型类与泛型方法的实现总结
  8. treepanel加滚动条
  9. Android手势锁实现
  10. Linux下配置C/C++开发环境-----Eclipse
  11. 阿里云ECS每天一件事D6:安装nginx-1.6.2
  12. BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)
  13. .NET Core 2.0 正式发布信息汇总
  14. 【转】非常实用的高频PCB电路设计70问
  15. redis-4.0.8 配置文件解读
  16. Jquery如何获取某个元素前(后)的文本内容?
  17. 好用到哭的listary
  18. React Native 炫酷的动画库 实现任何AE动画 lottie-react-native
  19. python 流程控制(while)
  20. 阿里云(一)云存储OSS的命令行osscmd的安装和使用

热门文章

  1. Git:pull --rebase 和 merge --no-ff
  2. MongoDB3.x中添加用户和权限控制
  3. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS
  4. Leetcode: Pow(x, n) and Summary: 负数补码总结
  5. linux常用命令:df 命令
  6. Js基础知识6-JavaScript匿名函数和闭包
  7. 百度云盘-真实地址 F12 控制台
  8. 深入hibernate的三种状态(转)
  9. 计算概论(A)/基础编程练习1(8题)/6:判断闰年
  10. Python Web学习笔记之TCP/IP、Http、Socket的区别