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. EMQ学习笔记---Clean Session和Retained Message

    MQTT会话(Clean Session)MQTT客户端向服务器发起CONNECT请求时,可以通过’Clean Session’标志设置会话.‘Clean Session’设置为0,表示创建一个持久会 ...

  2. 转: RabbitMQ实现中AMQP与MQTT消息收发异同

    转自:http://www.cnblogs.com/lucifer1997/p/9438186.html 实现了AMQP与MQTT(至多一次)后,用多个队列以topic exchange的方式用相同交 ...

  3. Spring MVC 框架搭建及具体解释

    如今主流的Web MVC框架除了Struts这个主力 外.其次就是Spring MVC了,因此这也是作为一名程序猿需要掌握的主流框架.框架选择多了.应对多变的需求和业务时,可实行的方案自然就多了. 只 ...

  4. 删除MYSQL账号多于的空用户

    默认情况下,mysql安装好之后,会存在匿名用户,也可以叫空用户,输入mysql之后直接回车便可进入mysql. 该匿名用户具有一定的权限,通过SHOW DATABASES;可以查看到informat ...

  5. css 禁用移动端部分特性

    禁止复制黏贴功能 .oncopy{ -webkit-user-select: none; } 禁止点击之后修改背景颜色 -webkit-tap-highlight-color: rgba(0,0,0, ...

  6. python得到今天前的七天每天日期

    import datetime d = datetime.datetime.now() def day_get(d): # 通过for 循环得到天数,如果想得到两周的时间,只需要把8改成15就可以了. ...

  7. JS prototype 属性

    String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}

  8. Nginx日志深入详解

    一.日志分类 Nginx日志主要分为两种:访问日志和错误日志.日志开关在Nginx配置文件(/etc/nginx/nginx.conf)中设置,两种日志都可以选择性关闭,默认都是打开的.1.访问日志 ...

  9. Vivado 自带IP仿真问题

    可以新建一个测试工程,通过IP catalog直接生产IP核,在IP核上右键选择 Open IP Example Design 之后选择生成路径. 启动Run Simulation.

  10. Redis_发布订阅(Spring Boot)

    目录 前言 生产者和消费者 发布和订阅 Java实现 注意 转至 http://www.tianmaying.com/tutorial/springboot-redis-message 前言 利用Sp ...