Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)
C. Reberland Linguistics
1 second
256 megabytes
standard input
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}.
The only line contains a string s (5 ≤ |s| ≤ 104) consisting of lowercase English letters.
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.
abacabaca
3
aca
ba
ca
abaca
0
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.
写了两天,才搞定。
in a row 是后来才注意到的,但是我还是认为不会影响什么,一直认为以前出现过就不行,最后问了学姐,给了一组样例。
zzzzz cf aaa aa aaa,不能出现相邻的相同的。
一下子考虑四个或者六个。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
using namespace std;
const int maxn = 1e4+;
int dp[maxn][];
set<string>ans;
int main()
{
string s;
cin>>s;
int n = s.size();
if(n<=)
{
printf("0\n");
return ;
}
if(n>=)
{
string s1="";
s1 = s1+s[n-]+s[n-];
ans.insert(s1);
dp[n-][] = ;
}
if(n>=)
{
string s1;
s1 = s1+s[n-]+s[n-]+s[n-];
ans.insert(s1);
dp[n-][] = ;
}
for(int i=n-;i>=;i--)
{
string s1="",s2 = "";
s1 = s1+s[i]+s[i+];
s2 = s2+s[i+]+s[i+];
// cout<<s1<<endl;
// cout<<s2<<endl;
if((s1!=s2&&dp[i+][])||dp[i+][])
{
dp[i][] = ;
ans.insert(s1);
// cout<<s1<<endl;
}
s1 = "";
s2 = "";
s1 = s1+s[i]+s[i+]+s[i+];
// cout<<s1<<endl;
s2 = s2+s[i+]+s[i+]+s[i+];
if((s1!=s2&&dp[i+][])||dp[i+][])
{
dp[i][] = ;
ans.insert(s1);
// cout<<s1<<endl;
}
}
printf("%d\n",ans.size());
for(set<string>::iterator it = ans.begin();it!=ans.end();it++)
{
cout<<*it<<endl;
}
return ;
}
Codeforces Round #349 (Div. 2) C. Reberland Linguistics (DP)的更多相关文章
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划
A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...
- 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 Round #349 (Div. 2) C. Reberland Linguistics DP+set
C. Reberland Linguistics First-rate specialists graduate from Berland State Institute of Peace a ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)
Coloring Trees Problem Description: ZS the Coder and Chris the Baboon has arrived at Udayland! They ...
- Codeforces Round #245 (Div. 1) B. Working out (dp)
题目:http://codeforces.com/problemset/problem/429/B 第一个人初始位置在(1,1),他必须走到(n,m)只能往下或者往右 第二个人初始位置在(n,1),他 ...
- Codeforces Round #260 (Div. 1) 455 A. Boredom (DP)
题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory l ...
- Codeforces Round #369 (Div. 2) C. Coloring Trees (DP)
C. Coloring Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #552 (Div. 3) F. Shovels Shop(dp)
题目链接 大意:给你n个物品和m种优惠方式,让你买k种,问最少多少钱. 思路:考虑dpdpdp,dp[x]dp[x]dp[x]表示买xxx种物品的最少花费,然后遍历mmm种优惠方式就行转移就好了. # ...
随机推荐
- Python 学习笔记3
人如果从来没有怀疑过自己,那他永远都不会进步. 今天学习Python解释器及其环境. http://www.pythondoc.com/pythontutorial3/interpreter.html
- android打包大小笔录
版本 4.6.1 1-1 游戏完整包的大小 1-2 1级代码压缩 1-3 2级代码压缩 1-4 3级代码压缩 2-1 删除核心战斗的部分资源 2-2 2级代码压缩 3-0 删除外部UI的资源 剩下 ...
- 用Jedis获取redis连接(集群和非集群状态下)
第一:非集群状态下 非集群状态下用Jedis获取redis连接,得到Jedis对象即可,一共有两种: 1.利用Jedis构造器,仅限用于测试,在实际项目中肯定是用JedisPool. Jedis(St ...
- 基于ZooKeeper的分布式Session实现
1. 认识ZooKeeper ZooKeeper—— “动物园管理员”.动物园里当然有好多的动物,游客可以根据动物园提供的向导图到不同的场馆观赏各种类型的动物,而不是像走在原始丛林里,心惊胆颤的被 ...
- memcached + php 扩展 for ubuntu
1.安装memcached apt-get install memcached 2.安装php memcached 扩展 apt-get install php5-memcache 3.启动memca ...
- chrome插件:打开新tab时自动打开百度
下载 安装: 解压到某一目录 如要修改自动跳转链接:修改go.js中的url (function (){ chrome.tabs.getCurrent(function(tab){ chrome.ta ...
- ios导航栏又按钮添加图片后使其保持原色
UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStyl ...
- linux命令积累(Ubuntu)
1.查看IP地址 ifconfig 2.退出more 使用ctrl+c 3.vi编辑,删除一行为dd 4.ubuntu安装tftp服务器:http://www.cnblogs.com/geneil/a ...
- jdk and tomcat 环境变量配置
一.安装JDK和Tomcat 1,安装JDK:直接运行jdk-7-windows-i586.exe可执行程序,默认安装即可. 备注:路径可以其他盘符,不建议路径包含中文名及特殊符号. 2.安装Tomc ...
- 编写程序,从vector<char>初始化string
#include<iostream> #include<string> #include<vector> using namespace std; int main ...