设串为str, 串长为len。

对整个串求一遍next函数,从串结尾开始顺着next函数往前找<=len/3的最长串,假设串长为ans,由于next的性质,所以找到的串肯定满足E……E这种形式,然后就是在str[ans]-str[len-2*ans]中查找是不是包含E,找到就输出,找不到就沿着next向下寻找,缩短串长。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ; char str[MAXN];
char tmp[MAXN];
int nextval[MAXN];
int len; void GetNextVal( char *s, int lenth )
{
int i = , j = -;
nextval[] = -;
while ( i < lenth )
{
if ( j == - || s[i] == s[j] )
{
++i, ++j;
if ( s[i] != s[j] ) nextval[i] = j;
else nextval[i] = nextval[j];
}
else j = nextval[j];
}
return;
} bool KMP( char *t, char *s, int lenth, int lenn )
{
//GetNextVal( t, lenth );
int i = , j = ;
while ( j < lenn )
{
if ( i == - || s[j] == t[i] )
{
++i, ++j;
if ( i == lenth ) return true;
}
else i = nextval[i];
//if ( i == lenth ) return true;
}
return false;
} int main()
{
int T;
scanf( "%d", &T );
while ( T-- )
{
scanf( "%s", str );
len = strlen(str);
GetNextVal( str, len );
int ans = nextval[len];
while ( ans > len/ ) ans = nextval[ans];
while ( ans > )
{
if ( KMP( str, &str[ans], ans, len - ans - ans ) )
break;
ans = nextval[ans];
}
if ( ans < ) printf("%d\n", len/);
else printf( "%d\n", ans );
}
return ;
}

最新文章

  1. js获取网页中宽高度集合
  2. 浅谈Android应用保护(一):Android应用逆向的基本方法
  3. 使用JS实现前端缓存
  4. css清楚浮动的方法
  5. How do you install mysql-connector-python (development version) through pip?
  6. 【ASH】如何导出视图DBA_HIST_ACTIVE_SESS_HISTORY的查询结果数据
  7. [转]lua面向对象编程之点号与冒号的差异详细比较
  8. Java 中方法的重载
  9. 嵌入式 hi3518平台多路码流添加osd
  10. editplus批量删除html代码空行
  11. OGR 官方文档
  12. U3D简单得换装技术
  13. 【录教程必备】推荐几款屏幕录制工具(可录制GIF)
  14. (转)示例化讲解RIP路由更新机制
  15. java多继承
  16. 转载 Elasticsearch开发环境搭建(Eclipse\MyEclipse + Maven)
  17. 4.1Python数据类型(1)之数值类型
  18. Qt中QSlider的样式表设置
  19. delphi 控制音量 静音的类
  20. PAT甲题题解-1068. Find More Coins (30)-dp,01背包

热门文章

  1. 今天 小小收获, 看了 sam Xiao 的好帖子 明白了 泛型委托 的 意思。
  2. glocktop
  3. 基于建模的视觉定位(SFM-Based Positioning)
  4. hihoCoder 网络流四&#183;最小路径覆盖
  5. Aspects– iOS的AOP面向切面编程的库
  6. iOS面试题总结(持续更新)
  7. LeetCode111. Minimum Depth of Binary Tree
  8. TCPIP协议编程:基于UDP协议的局域网聊天工具的研发
  9. scrapy--json(喜马拉雅Fm)(二)
  10. 最小生成数kruskal算法和prim算法