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种优惠方式就行转移就好了. # ...
随机推荐
- Shell脚本常用判断
-e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fi ...
- HDU 5933/思维
题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=5933]; 题意: 给出n堆物品,问能不能分成K个数量相等的堆,如果能分,则最少次数是多少.分的规则为: ...
- 查看当前目录每个文件的大小(linux)
du -sh * 查看当前目录每个文件的大小
- button的action属性如果有参数,必须加“:”
比如: [bt addTarget:self action:@selector(shareButtonClickHandler:) …… 后面未写完 一开始我没加,就报错.
- Oracle用户密码过期和用户被锁解决方法【转】
[原因/触发因素] 确定是由于Oracle11g中默认在default概要文件中设置了“PASSWORD_LIFE_TIME=180天”所导致. [影响和风险] 影响 密码过期后,业务进程连接数据库异 ...
- asp.net控件ControlToValidate同OnClientClick冲突解决办法
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Error ...
- C#生成DLL文件
使用csc命令将.cs文件编译成.dll的过程 很多时候,我们需要将.cs文件单独编译成.dll文件, 操作如下: 打开命令窗口->输入cmd到控制台->cd C:\WINDOWS\Mic ...
- 转一篇分析C语言调用时栈的变化的好文
http://blog.csdn.net/zsy2020314/article/details/9429707
- eclipse的调试方法的简单介绍
声明:本文不是自己 作为编程人员,程序的调试是一项基本功.在不使用IDE的时候,程序的调试多数是通过日志或者输入语句(System.out.println)的方式.可以把程序运行的轨迹或者程序运行过程 ...
- linux系统tomcat项目部署和tomcat访问日志
一.只用ip地址访问 先把端口号改成80,然后用 <Host name="localhost" appBase="webapps" 137 ...