CodeForces 666A Reberland Linguistics(DP)
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
动态规划,一开始看错了题目,题目的意思是不能有连续相邻的两个后缀是相同的
#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)的更多相关文章
- 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 Gym101341K:Competitions(DP)
http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...
- Codeforces.666A.Reberland Linguistics(DP)
题目链接 \(Description\) 给定串s,其由一个基本串后加任意多个长度为2或3的后缀串构成,要求基本串长度>4且相邻后缀串不相同.在基本串任意确定的情况下,求所有可能的后缀串. \( ...
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
- codeforces#1154F. Shovels Shop (dp)
题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...
- Codeforces 1051 D.Bicolorings(DP)
Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...
- Codeforces 1207C Gas Pipeline (dp)
题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...
- Codeforces 704C - Black Widow(dp)
Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这种题被评到 *2900 是因为细节太繁琐了,而不是题目本身的难度,所以我切掉这种题根本不能说明什么-- 首先题目中有一个非 ...
- Codeforces 682B New Skateboard(DP)
题目大概说给一个数字组成的字符串问有几个子串其代表的数字(可以有前导0)能被4整除. dp[i][m]表示字符串0...i中mod 4为m的后缀的个数 通过在i-1添加str[i]字符转移,或者以st ...
随机推荐
- HTML5使用canvas画图时,图片被自动放大模糊的问题
最近在研究canvas技术,发现一个问题,就是所画图像会随着画布大小自动变换大小.原因如下 <canvas id="cxt" style="width: 500px ...
- git 简单使用规范
分支管理办法 创建一个主仓库dev 每个成员fork一份dev分支 在自己fork出来的代码里做开发 开发完成后发出一个合并请求 pull request,等待被其他有合并权限的同事合并代码,合并代码 ...
- mysql init_connect
init_connect 服务器为每个连接的客户端执行的字符串.字符串由一个或多个SQL语句组成.要想指定多个语句,用分号间隔开.例如,每个客户端开始时默认启用autocommit模式.没有全局服务器 ...
- 多线程-AbstractQueuedSynchronizer(AQS)
概述 从使用者的角度,AQS的功能可分为两类:独占功能和共享功能.它的子类中,要么实现并使用了它独占功能的API,要么使用了共享锁的功能,而不会同时使用两套API,即使是它的子类ReentrantRe ...
- 多线程-ReentrantReadWriteLock
ReentrantLock具有完全互斥排他的效果,即同一时间只有一个线程在执行ReentrantLock.lock()方法后面的任务.这样做虽然保证了实例变量的线程安全,但效率却是非常低下的.JDK中 ...
- [容器]docker创建镜像
手动创建: docker run -d -p mynginx:v2 nginx rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest- ...
- iptables常用规则:屏蔽IP地址、禁用ping、协议设置、NAT与转发、负载平衡、自定义链
iptables常用规则:屏蔽IP地址.禁用ping.协议设置.NAT与转发.负载平衡.自定义链 时间 -- :: IT社区推荐资讯 原文 http://itindex.net/detail/4772 ...
- 李洪强和你一起学习前端之(7)定位盒子 css可见性 滑动门案例
今天是2017年3月23日 1 复习昨天知识 1.1浮动 Float:left | right 特点: ->浮动的元素不占位置(脱标) ->可以将行内元素转化为行内块元素 ->块级元 ...
- 微信小程度腾讯地图SDK使用方法
在开发过程中,不少人肯定遇到过要用到地图,那么在小程序里,腾讯也给出了相应的SDK供我们来使用.那么接下来,就介绍下如何使用该SDK实现获取经纬度然后显示当前用户所在地址 首先第一步:下载腾讯地图SD ...
- hdu 1000&hdu1001
1001 #include<iostream> #include<stdio.h> using namespace std; int main() { long long n; ...