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种优惠方式就行转移就好了. # ...
随机推荐
- Time complexity of ArrayList in Java
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add ope ...
- SQL函数学习(三):convert()函数
格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,c ...
- 怎么取消ie浏览器body与html的间隙
在css文件第一行定义全局样式,可以消除html标签默认间隙*{margin:0;padding:0;}
- CF/div2c/贪心
题目链接[http://codeforces.com/contest/749/problem/C] 题意:给出一个长度为n序列包含D和R,每一轮操作的先后顺序是1-n,规则是每一轮每个人有一次机会杀掉 ...
- ubuntu server 时区设置问题解决
1.当执行此命令的时候 ntpdate us.pool.ntp.org 出现一下错误提示 name server cannot be used: Temporary failure in name r ...
- FZU 1502 Letter Deletion
最长公共子序列. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- JDK,Tomcat,myeclipse相关资料
配置JDK(安装oracle必须先配好jdk): 1.右击计算机-属性-高级系统设置-高级-环境变量,弹出“环境变量”对话框.在Administrator的用户变量里添加: JAVA_HOME指明JD ...
- VMware+Windbg双机调试
虚拟机使用XP系统:
- VI/VIM 常用命令
VI/VIM 常用命令=========== 整理自鸟哥的私房菜 ---------- - 移动光标 命令 | 描述----------------------- ...
- 第十三节,基本数据类型,数字int字符串str
基本数据类型 数字 int 字符串 str 布尔值 bool 列表 list 元组 tuple 字典 dict 数据类型关系图 查看一个对象的类 如:如查看对象变量a是什么类 用到函 ...