Codeforces.666A.Reberland Linguistics(DP)
\(Description\)
给定串s,其由一个基本串后加任意多个长度为2或3的后缀串构成,要求基本串长度>4且相邻后缀串不相同。在基本串任意确定的情况下,求所有可能的后缀串。
\(|s|\leq 10^4\)。
\(Solution\)
把串反过来求前缀方便些。
明显的dp,f[i][2/3]表示当前到i,上一个后缀串长度为2/3是否可行。边转移边更新答案就行了。
#include <cstdio>
#include <cstring>
#include <algorithm>
#define pc putchar
const int N=10005;
int n;
bool f[N][2]/*0:2 1:3*/,ok2[28][28],ok3[28][28][28];
char s[N];
int main()
{
scanf("%s",s+1), n=strlen(s+1);
std::reverse(s+1,s+1+n);
int tot=0; n-=5;
if(n>=2) f[2][0]=1, ++tot, ok2[s[2]-'a'][s[1]-'a']=1;
if(n>=3) f[3][1]=1, ++tot, ok3[s[3]-'a'][s[2]-'a'][s[1]-'a']=1;
for(int i=4; i<=n; ++i)
{
int a=s[i]-'a', b=s[i-1]-'a', c=s[i-2]-'a';
if(f[i-2][1]||(f[i-2][0]&&(s[i]!=s[i-2]||s[i-1]!=s[i-3])))
{
f[i][0]=1;
if(!ok2[a][b]) ++tot, ok2[a][b]=1;
}
if(f[i-3][0]||(f[i-3][1]&&(s[i]!=s[i-3]||s[i-1]!=s[i-4]||s[i-2]!=s[i-5])))//i>=6
{
f[i][1]=1;
if(!ok3[a][b][c]) ++tot, ok3[a][b][c]=1;
}
}
printf("%d\n",tot);
for(int i=0; i<27&&tot; ++i)
{
for(int j=0; j<27; ++j)
{
if(ok2[i][j]) --tot,pc(i+'a'),pc(j+'a'),pc('\n');
for(int k=0; k<27; ++k)
if(ok3[i][j][k]) --tot,pc(i+'a'),pc(j+'a'),pc(k+'a'),pc('\n');
}
}
return 0;
}
Codeforces.666A.Reberland Linguistics(DP)的更多相关文章
- CodeForces 666A Reberland Linguistics(DP)
A. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces 667C Reberland Linguistics 记忆化搜索
链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp
题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...
- codeforces 667C C. Reberland Linguistics(dp)
题目链接: C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes inpu ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace a ...
- CodeForces 667C Reberland Linguistics
$dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...
- Codeforces 667C Reberland Linguistics【DFS】
一道卡题意的题. 题目链接: http://codeforces.com/problemset/problem/667/C 题意: 一个串可以看成一个长度大于4的根,加上其后面的若干个相邻(in a ...
- Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)
C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划
A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...
随机推荐
- Python常用模块-时间模块(time&datetime)
Python常用模块-时间模块(time & datetime) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.初始time模块 #!/usr/bin/env pyth ...
- 质数——6N±1法
6N±1法求素数 任何一个自然数,总可以表示成为如下的形式之一: 6N,6N+1,6N+2,6N+3,6N+4,6N+5 (N=0,1,2,…) 显然,当N≥1时,6N,6N+2,6N+3,6N+4都 ...
- Sparrow.Chart.Wpf控件的动态调用
最近需要在Wpf程序中显示曲线,感觉Sparrow.Chart.Wpf控件不错(http://sparrowtoolkit.codeplex.com/),完全开源的一个控件支持,可以通过nuget下载 ...
- ActiveMQ基础教程JMS概述
什么是JMS JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息 ...
- JavaScript内部原理实践——真的懂JavaScript吗?(转)
通过翻译了Dmitry A.Soshnikov的关于ECMAScript-262-3 JavaScript内部原理的文章, 从理论角度对JavaScript中部分特性的内部工作机制有了一定的了解. 但 ...
- mysql先排序在分组
– 表的结构 `test`– CREATE TABLE IF NOT EXISTS `test` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varcha ...
- 关于Asp.Net中的编程实现下载
经常在论坛看见有人求Asp.Net中编程实现下载的代码,有些还希望能断点续传什么的.其实问题的关键在于权限.B/S和C/S不仅仅是外观上的区别而已. 下载,顾名思义是客户端要下,所以载.你硬塞給人家那 ...
- 在OS X 10.9配置WebDAV服务器联合NSURLSessionUploa…
CHENYILONG Blog 在OS X 10.9配置WebDAV服务器联合NSURLSessionUploadTask实现文件上传iOS7推出的NSURLSession简化了NSURLConn ...
- hibernate的多对多关联映射
在我们实际项目中,多对多的情况也时长存在,比如最常见的就是系统管理的五张表,如下面的一个结构: 在本文学习hibernate多对多关联映射的实验中我简单的写几个字段,达到学习目的即可. 1.多对多的关 ...
- thymeleaf:访问静态方法
<p class="left tel" th:if="${#strings.startsWith(T(net.common.util.tool.common.Req ...