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

动态规划,一开始看错了题目,题目的意思是不能有连续相邻的两个后缀是相同的

#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <stdio.h>
#include <map>
#include <string> using namespace std;
#define MAX 10000
struct Node
{
string a;
}a[MAX*10];
int cmp(Node a,Node b)
{
return a.a<b.a;
}
int dp[MAX+5];
string s;
map<string,int> m;
int main()
{
cin>>s;
int len=s.length();
memset(dp,0,sizeof(dp));
dp[len]=1;dp[len-1]=0;dp[len+1]=0;
m.clear();
int cnt=0;
for(int i=len-2;i>=5;i--)
{
if(dp[i+2]!=0)
{
string b=s.substr(i,2);
if(dp[i+2]!=2||b!=s.substr(i+2,2))
{
if(!m.count(b))
{
m[b]=1;
a[cnt++].a=b;
}
dp[i]+=2;
}
}
if(dp[i+3]!=0)
{
string b=s.substr(i,3);
if(dp[i+3]!=3||b!=s.substr(i+3,3))
{
if(!m.count(b))
{
m[b]=1;
a[cnt++].a=b;
}
dp[i]+=3;
}
}
}
sort(a,a+cnt,cmp);
printf("%d\n",cnt);
for(int i=0;i<cnt;i++)
cout<<a[i].a<<endl;
return 0; }

CodeForces 666A Reberland Linguistics(DP)的更多相关文章

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

  2. Codeforces Gym101341K:Competitions(DP)

    http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...

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

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

  4. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  5. codeforces#1154F. Shovels Shop (dp)

    题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...

  6. Codeforces 1051 D.Bicolorings(DP)

    Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...

  7. Codeforces 1207C Gas Pipeline (dp)

    题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...

  8. Codeforces 704C - Black Widow(dp)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这种题被评到 *2900 是因为细节太繁琐了,而不是题目本身的难度,所以我切掉这种题根本不能说明什么-- 首先题目中有一个非 ...

  9. Codeforces 682B New Skateboard(DP)

    题目大概说给一个数字组成的字符串问有几个子串其代表的数字(可以有前导0)能被4整除. dp[i][m]表示字符串0...i中mod 4为m的后缀的个数 通过在i-1添加str[i]字符转移,或者以st ...

随机推荐

  1. 改动文件后缀的C语言实现

    ,其他配置项保持一致.         step 3: 在"Old2New"目录下新建名为"update.bat"的批处理文件,该文件的内容为: ChangeS ...

  2. Easyui 二级菜单

    <div class="fitem"> <label>所在城市:</label> <input id="cityId" ...

  3. Hive 数据倾斜原因及解决方法(转)

    在做Shuffle阶段的优化过程中,遇到了数据倾斜的问题,造成了对一些情况下优化效果不明显.主要是因为在Job完成后的所得到的Counters是整个Job的总和,优化是基于这些Counters得出的平 ...

  4. Spring Boot(三):logback打印日志

    springboot对logback的支持是非常好的,不需要任何配置,只需要在resource下加logback.xml就可以实现功能直接贴代码: <?xml version="1.0 ...

  5. Spark-shell 无法启动之网络问题

    由于需要首次手动安装sbt,需要联网,故将虚拟机的网络适配器模式设置为"桥接模式",这样就可以和互联网相连接. 但是后面执行"spark-shell  --master ...

  6. Atitit.jquery 版本新特性attilax总结

    Atitit.jquery 版本新特性attilax总结 1. Jq1.4 1 2. 1.5 1 3. 1.6 3 4. Jq1.7 3 ⒉提升了事件委派时的性能有了大幅度的提升,尤其是在ie7下: ...

  7. JSON 常用数据转换

    #endregion #region Json字符串转换为DataTable 实例方法 public DataTable JsonToDataTable(json) { DataTable dt= T ...

  8. ToStringBuilder学习(二):两种方法用法优缺点及一个问题

    研究ApacheCommon源码, 先从一个最简单的开始,即围绕Object类里的toString方法自动化实现的一系列类.         怎么来自动化地实现toString方法, 有两种:反射和手 ...

  9. freemarker和jsp的比较

    1.共享变量 FreeMarker 的共享变量是我最喜欢的“隐藏”功能之一.此功能可以让你设置自动添加到所有模板的值. 例如,可以设置应用程序的名称作为共享变量. 1 2 Configuration ...

  10. HTML5七巧板canvas绘图

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...