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 ...
随机推荐
- bzoj千题计划212:bzoj1864: [Zjoi2006]三色二叉树
http://www.lydsy.com/JudgeOnline/problem.php?id=1864 #include<cstdio> #include<cstring> ...
- win10内建子系统Linux
从cmd中下载linux和linux终端安装程序一样 最新要从商店购买(当然是免费) ubuntu openSUSE SUSE 3个 创建用户
- Redis五种数据结构(Windows Server)
1.Redis的五种数据结构 这里推荐大家在命名redis的key的时候最好的加上前缀,并且使用 :来分割前缀 ,这里在使用可视化工具查看的时候就比较好区分,比如我的的前缀是 Demo:test:(一 ...
- js 语法高亮插件之 Prism.js
之前也介绍过几款语法高亮插件<为博客园选择一个小巧霸气的语法高亮插件>以及关于他们的综合性能<再议 语法高亮插件的选择>.今天在小影志博客看到<使用 Prism.js 实 ...
- CSS规范 - 命名规则--(来自网易)
使用类选择器,放弃ID选择器 ID在一个页面中的唯一性导致了如果以ID为选择器来写CSS,就无法重用. NEC特殊字符:"-"连字符 "-"在本规范中并不表示连 ...
- 微信公众号用户OpenID同步导出系统
一.简介 同步公众账号用户信息,包括OpenID.昵称.头像.地区等. 二.主要功能 同步公众账号用户 OpenID,以及昵称.头像.性别.地区.关注时间等,支持认证订阅号.认证服务号. 支持超过1万 ...
- HTML5页面开发的基础性模板
分享一个HTML5页面开发的基础性模板,包含了两个版本: 开发版本 注释版本 开发版本 <!DOCTYPE html> <html> <head> <meta ...
- HDU 4472 Count DP题
解题报告:题目大意,给你n个球,要将这n个球从下到上按层次排列,要求同一个层次的的每一个分支的数量都必须相等,问有多少种排列的方法. 此题的一个DP题,假设现在有n个球,要将这n个球排列好,我们就必须 ...
- python中的 __repr__和__str__
__repr__,被内置函数repr用于把一个对象用"官方"的字符串形式表示出来(终端友好) 1.值传给eval()来返回一个对象的字符串表示形式 2.否则返回一个尖括 ...
- sqlserver2008R2数据库自动备份脚本
CREATE proc [dbo].[usp_autoBackupDB] @dbname sysname=null --要备份的数据库名,不指定即为全部备份 ,)='d:\' --备份目录路径 ,)= ...