http://acm.hdu.edu.cn/showproblem.php?pid=4763

题目大意:给一个字符串,判断是否可以写成ABACA,B、C表示长度大于等于0的字符串。

方法:ans = next[len]如果小于等于len/3,则ans是最大可能的答案,否则ans = next[ans] 要继续往前走,直到ans <= len/3, 然后枚举从2*ans位置枚举到len - ans,看是否存在某个位置j, 使得next[j] = ans,当然这里也有一个往前走的过程(如果next[j]一开始大于ans),具体见代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <stack>
#include <string>
#include <ctime>
#include <queue>
#define mem0(a) memset(a, 0, sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define eps 0.0000001
#define lowbit(x) ((x) & -(x))
#define memc(a, b) memcpy(a, b, sizeof(b))
#define x_x(a) ((a) * (a))
#define LL long long
#define DB double
#define pi 3.14159265359
#define MD 10000007
#define INF (int)1e9
#define max(a, b) ((a) > (b)? (a) : (b))
using namespace std;
char str[];
int next[];
void getNext()
{
next[] = next[] = ;
for(int i = ; str[i]; i++) {
int j = next[i];
while(j && str[i] != str[j]) j = next[j];
next[i + ] = str[i] == str[j]? j + : ;
}
}
int solve()
{
int len = strlen(str), ans = next[len], F = ;
while(ans * > len) ans = next[ans];
while(ans) {
for(int i = ans * ; i <= len - ans; i++) {
int j = next[i];
while(j > ans) j = next[j];
if(j == ans) {
F = ;
break;
}
}
if(F) break;
ans = next[ans];
}
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin);
int T;
cin>> T;
for(int i = ; i <= T; i++) {
scanf("%s", str);
getNext();
cout<< solve()<< endl;
}
return ;
}

最新文章

  1. UITabBarController 基本定制
  2. 上下箭头选中 选项事件 JS
  3. eclipse常用快捷键及调试方法(虽然现在看不懂,但是感觉以后肯定会用到,先转了)
  4. Android开发之SD卡上文件操作
  5. BZOJ_1016_[JSOI2008]_最小生成树计数_(dfs+乘法原理)
  6. js实现相册翻页,滚动,切换,轮播功能
  7. 其他应用和技巧-eval()函数大行其道
  8. android xml文件中出现如下提醒:This tag and its children can be replaced by one &lt;TextView/&gt; and a compound drawable
  9. js函数中的BOM和DOM
  10. angular-ui-alert
  11. vue源码分析—Vue.js 源码目录设计
  12. MNIST数字识别问题
  13. (深度好文)重构CMDB,避免运维之耻
  14. [转] Python Traceback详解
  15. 6-4 破碎的键盘 uva11988
  16. Composite组合模式(结构型模式)
  17. Tween.js 动画效果
  18. Erlang/Elixir: 使用 OpenCV, Python 搭建图片缩略图服务器
  19. c#省市联动(sqlHelper的应用)
  20. 【经验】实现STL算法时遇到的模板编译错误问题

热门文章

  1. 带权值的LCA
  2. sublime查看项目代码多少行
  3. 吃瓜的正确姿势,Python绘制罗志祥词云图
  4. SpringBoot word 转换为 pdf
  5. [Asp.Net Core] Blazor Server Side 项目实践 - 切换页面时保留状态
  6. 用Python的Plotly画出炫酷的数据可视化(含各类图介绍,附代码)
  7. token认证和理解
  8. PHP把PNG图片转化为JPG时透明背景变黑色
  9. 小程序里button边框有黑线解决办法(自定义button样式)
  10. zabbix 微信告警机制