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. 为select的option绑定键盘事件

    1. 目的 可以使用快捷键1.2.3.4等自动选中select框对应的option 2. 代码 <select id="selectItem" class="for ...

  2. ML中Boosting和Bagging的比較

    说到ML中Boosting和Bagging,他们属于的是ML中的集成学习,集成学习法(Ensemble Learning) ①  将多个分类方法聚集在一起.以提高分类的准确率. (这些算法能够是不同的 ...

  3. [转载]一种高性能Hierarchical RBAC实现方案

    背景 框图 上图中,Role和被设置Permission的Resource都是可以有任意层级继承关系的. 举例 举一个网站的例子来说: 如果,User表示网站用户:Role表示角色:Resource表 ...

  4. bootstrap 的使用

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  5. oracle数据库访问order by不起作用分析

    `SELECT * FROM student ROWNUM <= 1 ORDER BY id ASC`执行结果,返回结果没有排序.使用驱动"System.Data.OracleClie ...

  6. 跟着百度学PHP[15]-SESSION的应用/网站登陆案例完整案例

    先把几个应该要有的页面建立好.

  7. Python 中 global、nonlocal的使用

    1.在学习python过程中很重要的一点需要记住:如果在函数内部定义了跟全局变量同名的变量,那么该变量将是局部变量,而全局变量的引用在该函数范围内将失效. x = 9 def a(): x = 10 ...

  8. [转]wordpress安装插件的3种方式

    WordPress插件安装方法有几种?WordPress是一种使用PHP语言开发的博客平台,有些用户不知道怎么安装WordPress插件和主题的,所以今天小编就为大家介绍几种WordPress插件安装 ...

  9. python read文件内容的iter方式

    遍历file的方式 iter(lambda: f.read(4096), "")等价与while True: data = f.read(4096) if not data: br ...

  10. dedecms安全篇:织梦文件夹目录权限设置

    织梦各个目录安全详解   做织梦(dedecms)网站安全必看1.a  因为是静态目录,并且在要生成HTML的,所以拒绝脚本执行  允许写入2.data   因为是缓存等,所以充许写入,但是因为这里面 ...