Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set
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
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.
题意:
给你一串字符串,将后长与5的(只要在5后面)后半段分为只含三个或者两个的字母的连续后缀
后缀:满足不连续相同可以间隔相同;
将所有满足条件的分法的所有后缀放入集合,按字典序输出
题解:



#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+, M = 1e6+, mod = 1e9+, inf = 1e9+;
typedef long long ll; char a[N];
set<string > s;
int f[N][];//i开始到l是否可行
int main() {
scanf("%s",a);
int n = strlen(a);
a[n] = '';
a[n+] ='';
a[n+] = '';
if(n<=) {
cout<<<<endl;
return ;
}
if(n->=) {
string tmp;
tmp = tmp + a[n-]+ a[n-];
s.insert(tmp);
f[n-][] = ;
}
if(n->=) {
string tmp;
tmp = tmp + a[n-]+ a[n-]+ a[n-];
s.insert(tmp);
f[n-][] = ;
}
for(int i=n-;i>=;i--) {
string s1 = "",s2 = "";
s1 = s1 + a[i] + a[i+];
s2 = s2 + a[i+] + a[i+];
if((s1!=s2&&f[i+][])||f[i+][]) {
f[i][] = ;
s.insert(s1);//cout<<s1<<endl;
}
if(i+<n) {
s1 = "",s2 = "";
s1 = s1 + a[i] + a[i+] + a[i+];
s2 = s2 + a[i+] + a[i+] + a[i+];
if((s1!=s2&&f[i+][])||f[i+][]) {
f[i][] = ;
s.insert(s1);
//cout<<s1<<endl;
}
}
}
cout<<s.size()<<endl;
for(set<string > ::iterator it=s.begin();it!=s.end();it++) cout<<(*it)<<endl;
return ;
}
Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set的更多相关文章
- 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. 1) A. Reberland Linguistics 动态规划
A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...
- 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 ...
- Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)
题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...
- Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举
题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...
- Codeforces Round #349 (Div. 2) D. World Tour (最短路)
题目链接:http://codeforces.com/contest/667/problem/D 给你一个有向图,dis[i][j]表示i到j的最短路,让你求dis[u][i] + dis[i][j] ...
- Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路
B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...
- Codeforces Round #349 (Div. 1)E. Forensic Examination
题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...
- Codeforces Round #349 (Div. 2)
第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...
随机推荐
- MySQL Connector for .NET 和 EF版本匹配问题
以下讨论的都是EF5.0, 版本号:4.4.0.0 如果装了MySQL 5.0.1 , 那么最好用MySQL Connector 6.3.6,但是创建数据库后,生成迁移历史表的时候,会报错,你不管,直 ...
- bootstrap的栅格系统和响应式工具
关于bootstrap的响应式布局,昨天看了杨老师的视频教学https://www.bilibili.com/video/av18357039豁然开朗,在这里记录一下 一:meta标签的引用 < ...
- 【HTTP】长连接和短连接
1. HTTP协议与TCP/IP协议的关系 HTTP的长连接和短连接本质上是TCP长连接和短连接.HTTP属于应用层协议,在传输层使用TCP协议,在网络层使用IP协议.IP协议主要解决网络路由和寻址问 ...
- 解决Latex复制到公众号可能报“图片粘贴失败”的问题
前几天出了个版本,还发了篇“Md2All,让公众号完美显示Latex数学公式”的文章,发完后,心里还是不太爽的,因为那个版本还是遗留了一个问题:当把Latex公式转换为本地图片,再复制到公众号时,有可 ...
- ★Java语法(一)——————————标识符
1.定义:用来标识类名.变量名.方法名.数组名.文件名的有效字符序列: 2.命名规则:a 由字母.数字._(下划线)和$(美元符号)组成 b 数字不能作为第一个字符 c 不能是Java中的关键字和保留 ...
- 【Oracle】表连接三种方式
表连接的方式有三种分别是:排序合并连接(Sort Merge Join).嵌套循环连接(Nested Loops Join).哈希连接(Hash Join). 1. 排序合并连接(Sort Merge ...
- halcon 模板匹配 -- create_shape_model
create_shape_model(Template : : //reduce_domain后的模板图像 NumLevels,//金字塔的层数,可设为“auto”或0—10的整数 AngleStar ...
- 利用string 字符串拷贝
序言:对于laws的代码,完全从Matlab中转来.其中用到了字符串复制和对比的函数. C++要求: 输入字符串,根据字符串,来确定选择数组,用于下一过程 MatLab代码: (1).文件calLaw ...
- java rsa加密解密
- eslint推荐编码规范和airbnb推荐编码规范
Eslint规范 for 循环禁止使用无限循环(这个非默认推荐) // bad for (var i = 0; i < 10; i--) { } for (var i = 10; i >= ...