题目链接:

C. Reberland Linguistics

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

First-rate specialists graduate from Berland State Institute of Peace and Friendship. You are one of the most talented students in this university. The education is not easy because you need to have fundamental knowledge in different areas, which sometimes are not related to each other.

For example, you should know linguistics very well. You learn a structure of Reberland language as foreign language. In this language words are constructed according to the following rules. First you need to choose the "root" of the word — some string which has more than 4 letters. Then several strings with the length 2 or 3 symbols are appended to this word. The only restriction — it is not allowed to append the same string twice in a row. All these strings are considered to be suffixes of the word (this time we use word "suffix" to describe a morpheme but not the few last characters of the string as you may used to).

Here is one exercise that you have found in your task list. You are given the word s. Find all distinct strings with the length 2 or 3, which can be suffixes of this word according to the word constructing rules in Reberland language.

Two strings are considered distinct if they have different length or there is a position in which corresponding characters do not match.

Let's look at the example: the word abacabaca is given. This word can be obtained in the following ways: , where the root of the word is overlined, and suffixes are marked by "corners". Thus, the set of possible suffixes for this word is {aca, ba, ca}.

Input
 

The only line contains a string s (5 ≤ |s| ≤ 104) consisting of lowercase English letters.

Output
 

On the first line print integer k — a number of distinct possible suffixes. On the next k lines print suffixes.

Print suffixes in lexicographical (alphabetical) order.

Examples
 
input
abacabaca
output
3
aca
ba
ca
input
abaca
output
0
Note

The first test was analysed in the problem statement.

In the second example the length of the string equals 5. The length of the root equals 5, so no string can be used as a suffix.

题意

给一个字符串,去掉一个长度至少为5的前缀,剩下的切成长度为2或者3的串,且相邻的串不相同,问,最后可以切成多少个串且分别是什么;

思路

dp[i][2]==1表示i,i+1形成的串可以满足要求,dp[i][3]==1同理;再就是转移了;

考虑i位置,长度为2;

(1)如果<i,i+1>与<i+2,i+3>相同,那么只有dp[i+2][3]==1时dp[i][2]=1;

如果<i,i+1>与<i+2,i+3>不同,那么只要dp[i+2][2]==1或者dp[i+2][3]==1时dp[i][2]=1;

(2)如果<i,i+1,i+2>与<i+3,i+4,i+5>相同,只有dp[i+3][2]==1时dp[i][3]=1;

如果不同,那么只要dp[i+3][2]==1或者dp[i+3][3]==1时dp[i][3]=1;

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e4+;
const LL mod=1e9+;
const double PI=acos(-1.0);
char str[N];
int dp[N][];
map<string,int>mp;
vector<string>ve;
int main()
{
scanf("%s",str);
int n=strlen(str);
string x,y="";
int ans=;
for(int i=n-;i>;i--)
{
if(n-i==)
{
x=y+str[i]+str[i+];
mp[x]=;
ve.push_back(x);
dp[i][]=;
}
else if(n-i==)
{
x=y+str[i]+str[i+]+str[i+];
mp[x]=;
ve.push_back(x);
dp[i][]=;
}
else if(n-i==)
{
if(str[i]!=str[i+]||str[i+]!=str[i+])
{
x=y+str[i]+str[i+];
if(!mp[x])
{
mp[x]=;
ve.push_back(x);
}
dp[i][]=;
}
else
{
x=y+str[i]+str[i+];
dp[i][]=;
}
}
else
{ if(str[i]==str[i+]&&str[i+]==str[i+])
{
if(dp[i+][]){
dp[i][]=;
x=y+str[i]+str[i+];
if(!mp[x])
{
mp[x]=;
ve.push_back(x);
}
}
}
else
{
if(dp[i+][]||dp[i+][])
{
dp[i][]=;
x=y+str[i]+str[i+];
if(!mp[x])
{
mp[x]=;
ve.push_back(x);
}
}
}
if(str[i]==str[i+]&&str[i+]==str[i+]&&str[i+]==str[i+])
{
if(dp[i+][])
{
dp[i][]=;
x=y+str[i]+str[i+]+str[i+];
if(!mp[x])
{
mp[x]=;
ve.push_back(x);
}
}
}
else
{
if(dp[i+][]||dp[i+][])
{
dp[i][]=;
x=y+str[i]+str[i+]+str[i+];
if(!mp[x])
{
mp[x]=;
ve.push_back(x);
}
}
}
}
}
ans=ve.size();
sort(ve.begin(),ve.end());
printf("%d\n",ans);
for(int i=;i<ans;i++)
{
cout<<ve[i]<<"\n";
} }

codeforces 667C C. Reberland Linguistics(dp)的更多相关文章

  1. 【16.67%】【codeforces 667C】Reberland Linguistics

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  2. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

  3. Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

    C. Reberland Linguistics     First-rate specialists graduate from Berland State Institute of Peace a ...

  4. Codeforces.666A.Reberland Linguistics(DP)

    题目链接 \(Description\) 给定串s,其由一个基本串后加任意多个长度为2或3的后缀串构成,要求基本串长度>4且相邻后缀串不相同.在基本串任意确定的情况下,求所有可能的后缀串. \( ...

  5. Codeforces 667C Reberland Linguistics 记忆化搜索

    链接 Codeforces 667C Reberland Linguistics 题意 给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复.找到所有的词根 思路 ...

  6. 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 ...

  7. CodeForces 666A Reberland Linguistics(DP)

    A. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input stan ...

  8. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  9. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

随机推荐

  1. PHP的Trait机制

    PHP的Trait机制 Trait介绍: 1.自PHP5.4起,PHP实现了一种代码复用的方法,称为trait.2.Trait是为类似PHP的单继承语言二准备的一种代码复用机制.3.Trait为了减少 ...

  2. Gradle讲解

    简介: Gradle是一个基于Apache Ant和Apache Maven概念的项目自动化构建工具.它使用一种基于Groovy的特定领域语言(DSL)来声明项目设置,抛弃了基于XML的各种繁琐配置. ...

  3. foobar2000播放APE格式音乐的解决办法

    要使foobar2000能播放APE格式的音乐需要安装Monkey's Audio Decoder的插件.操作如下: 1.登录官方插件列表,找到Monkey's Audio Decoder插件 官方插 ...

  4. tcp-time-wait-state

    https://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux

  5. Go -- PipleLine

    1.pipeline的产生 从一个现象说起,有一家咖啡吧生意特别好,每天来的客人络绎不绝,客人A来到柜台,客人B紧随其后,客人C排在客人B后面,客人D排在客人C后面,客人E排在客人D后面,一直排到店面 ...

  6. 【Todo】Java Callable和Future学习

    参考这篇文章:http://blog.csdn.net/ghsau/article/details/7451464  还有一个系列<Java多线程>

  7. 在Nginx上部署ThinkPHP,解决Pathinfo问题

    在Nginx上部署ThinkPHP,解决Pathinfo问题 事实上.要解决nginx不支持pathinfo的问题.有两个解决思路,一是不使用pathinfo模式,二是改动nginx的配置文件,使它支 ...

  8. Ubuntu下编译Android JNI实例全过程

    第一步:保证make和gcc可用 在shell中输入make-v.不报错就是对的.(可參考http://wenku.baidu.com/view/d87586c24028915f804dc24a.ht ...

  9. Cocoa root class

    问题来源: 常见面试问题之: NSObject和NSObject protocol有什么区别,为什么要有NSObject protocol, 有没有不继承自NSObject的类? 虽然在iOS开发过程 ...

  10. 【转载】C# sleep 和wait的区别

    eep和wait都是使线程暂时停止执行的方法,但它们有很大的不同. 1. sleep是线程类Thread 的方法,它是使当前线程暂时睡眠,可以放在任何位置. 而wait,它是使当前线程暂时放弃对象的使 ...