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 ...
随机推荐
- Java基础-数据类型应用案例展示
Java基础-数据类型应用案例展示 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.把long数据转换成字节数组,把字节数组数据转换成long. /* @author :yinz ...
- 浅说搜索引擎和SEO
搜索引擎 基本工作原理 信息收集功能 技术点SEO优化核心 优化重点 SEO优化 网站URL title信息 meta信息 图片ALT flash信息 frame框架 网页重要度特征 之前有看到一个师 ...
- bzoj千题计划205:bzoj3529: [Sdoi2014]数表
http://www.lydsy.com/JudgeOnline/problem.php?id=3529 有一张n*m的数表,其第i行第j列(1 < =i < =n,1 < =j & ...
- WebSockets Tutorial(教程一)WebSockets简介
一.WebSockets简介 以字面意思来说,握手可以被定义为两个人抓住和握手右手,象征着问候,祝贺,同意或告别.在计算机科学中,握手是确保服务器与客户端同步的过程.握手是Web Socket协议的基 ...
- Git Pull Failed: cannot lock ref 'refs/remotes/origin/xxxxxxxx': unable to resolve ref
1.xxxxxxxx代表目录名称,我要pull的目录是supman_creditmall_v5: 2.从代码库中pull代码时报这个错误,代码pull失败: 3.解决办法,看下图,删除文件后再pull ...
- SSH 公钥登录
一般使用SSH进行远程登录时需要提供密码,这也是我们所熟知的一种方式. 另外,就是通过公钥登录的方式,本文将简要介绍公钥登录的两种方法,建议使用方法二.本文也将简单演示公钥登录过程,以及强制使用公钥和 ...
- 日常训练赛 Problem C – Complete Naebbirac’s sequence
比赛链接https://vjudge.net/contest/256988#status/17111202012/C/0/ 大意:三个操作,使得输入的数中,从1-n,每一个数出现的次数相同. wa代码 ...
- 05 uni-app框架学习:uni-app设置全局变量的方法
原文地址:https://ask.dcloud.net.cn/article/35021
- IL反编译的实用工具Ildasm.exe
初识Ildasm.exe——IL反编译的实用工具 https://www.cnblogs.com/yangmingming/archive/2010/02/03/1662307.html 学 ...
- keepalived vrrp_script脚本不执行解决办法
首先打开日志观察: tail -f /var/log/messages 然后新开一个客户端重启keepalived , systemctl restart keepalived.service 看日志 ...