题目:

It's time for music! A lot of popular musicians are invited to join us in the music festival. Each of them will play one of their representative songs. To make the programs more interesting and challenging, the hosts are going to add some constraints to the rhythm of the songs, i.e., each song is required to have a 'theme section'. The theme section shall be played at the beginning, the middle, and the end of each song. More specifically, given a theme section E, the song will be in the format of 'EAEBE', where section A and section B could have arbitrary number of notes. Note that there are 26 types of notes, denoted by lower case letters 'a' - 'z'.

To get well prepared for the festival, the hosts want to know the maximum possible length of the theme section of each song. Can you help us?

Input

The integer N in the first line denotes the total number of songs in the festival. Each of the following N lines consists of one string, indicating the notes of the i-th (1 <= i <= N) song. The length of the string will not exceed 10^6.

Output

There will be N lines in the output, where the i-th line denotes the maximum possible length of the theme section of the i-th song. 
Sample Input

5
xy
abc
aaa
aaaaba
aaxoaaaaa

Sample Output

0
0
1
1
2
题意描述:
题目很有意思,输入一个字符串
计算并输出该串中“主题章节”的长度
解题思路:
属于KMP的next[]数组的应用。数据很水,放心提交。
代码实现:
 /*
判断一个字符是否满足EAEBE,其中E为相同,满足输出最大的E的长度
找到EAEBE的相似度t=next[l],在找AEB中是否存在一个相似度为t,如果存在且t不等于0输出t,否则存在特殊情况
比如a
aa
aaa
结果均是l/3
*/
#include<stdio.h>
#include<string.h>
char s[];
int next[],l;
int get_next(char t[]);
int main()
{
int n,t,flag,i;
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
l=strlen(s);
get_next(s);
t=next[l];
for(flag=,i=t;i<=l-t;i++)
{
if(next[i]==t)
flag=;
}
if(!flag || !t)
{
if(t+==l)
printf("%d\n",l/);
else
printf("0\n");
}
else
printf("%d\n",t);
}
return ;
}
int get_next(char t[])
{
int i,j;
i=;j=-;
next[]=-;
while(i < l)
{
if(j==- || t[i]==t[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
/*for(i=0;i<=l;i++)
printf("%d ",next[i]);
printf("\n");*/
}

 

最新文章

  1. 窥探Swift之别样的枚举类型
  2. ELF动态链接
  3. SQL知识整理三:变量、全局变量、视图、事务、异常
  4. Daily Scrum 12.14
  5. Okio 1.9简单入门
  6. Base64 算法原理,以及编码、解码【加密、解密】 介绍
  7. 16、C++获取磁盘空间的方法
  8. java_stack
  9. 在Linux下如何用Shell脚本读写XML?现有一个config.xml(转)
  10. angularJS directive详解(自定义指令)
  11. 三大修饰符static,final,abstract,接口和抽象类的区别
  12. python中修改字符串的几种方法
  13. 理解依赖注入,laravel IoC容器
  14. php json_encode与json_decode详解及实例
  15. BEX5下增加sessionStorage监听器实现页面间数据刷新
  16. Lua table笔记
  17. codevs3044
  18. LINQ to Objects系列(3)深入理解Lambda表达式
  19. 2018-10-18 22:15:32 c language
  20. [Web 前端] Jquery 复制元素,并修改属性, 追加到另一个元素后面

热门文章

  1. Bmob 移动后端云服务器平台实现登录注册
  2. Linux(CentOS6.5)下编译安装PHP5.6.22时报错&rdquo;configure: error: ZLIB extension requires gzgets in zlib&rdquo;的解决方式(确定已经编译安装Zlib,并已经指定Zlib路径)
  3. 分享非常好用的前端分页js工具类 灵活 简单易懂
  4. K:java中properties文件的读写
  5. Java核心技术(Java白皮书)卷Ⅰ 第一章 Java程序设计概述
  6. sqlalchemy 踩过的坑
  7. 使用alembic进行数据库版本管理
  8. mysql启动日志文件log_bin
  9. ThreadPool.QueueUserWorkItem引发的血案,线程池异步非正确姿势导致程序闪退的问题
  10. 第五章:Python基础の生成器、迭代器、序列化和虚拟环境的应用