题目链接:

题目

A. Reberland Linguistics

time limit per test:1 second

memory limit per test:256 megabytes

问题描述

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.

样例

input

abacabaca

output

3

aca

ba

ca

题意

英语渣orz不知道“twice in a row”是指连续两个的意思。。

先把串的开头5个去掉,把剩下的分割为长度为2,3的若干个子串,并且任何相邻的子串不能相同,问最后能形成的所有合法的不同子串。

题解

dp[i][0]表示以i结尾的长度为2的子串是否能分割出来。

dp[i][1]表示以i结尾的长度为3的子串是否能分割出来。

则有状态转移:

dp[i][0]=dp[i-2][1]||dp[i-2][0]&&(str[i]!=str[i-2]||str[i-1]!=str[i-3])

dp[i][1]类似上面的转移。

代码

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std; const int maxn = 1e4 + 10; string str;
int n;
bool dp[maxn][2]; int main() {
cin >> str;
n = str.length();
reverse(str.begin(),str.end());
memset(dp, 0, sizeof(dp));
if(n>=7) dp[1][0] = 1;
if (n >= 8) dp[2][1] = 1;
for (int i = 3; i < n - 5; i++) {
if (dp[i - 2][1]) {
dp[i][0] = 1;
}
if (dp[i - 2][0] && !(str[i] == str[i - 2] && str[i - 1] == str[i - 3])) {
dp[i][0] = 1;
}
if (dp[i - 3][0]) {
dp[i][1] = 1;
}
if (dp[i - 3][1] && !(str[i] == str[i - 3] && str[i - 1] == str[i - 4] && str[i - 2] == str[i - 5])) {
dp[i][1] = 1;
}
}
vector<string> ans;
string s;
for (int i = 0; i < n - 5; i++) {
if (dp[i][0]) {
s = ""; s += str[i]; s += str[i - 1];
ans.push_back(s);
}
if (dp[i][1]) {
s = ""; s += str[i]; s += str[i - 1]; s += str[i - 2];
ans.push_back(s);
}
}
sort(ans.begin(), ans.end());
ans.erase(unique(ans.begin(), ans.end()),ans.end());
cout << ans.size() << endl;
for (int i = 0; i < ans.size(); i++) {
cout << ans[i] << endl;
}
return 0;
}

Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp的更多相关文章

  1. Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set

    C. Reberland Linguistics     First-rate specialists graduate from Berland State Institute of Peace a ...

  2. Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划

    A. Reberland Linguistics 题目连接: http://www.codeforces.com/contest/666/problem/A Description First-rat ...

  3. 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 ...

  4. Codeforces Round #174 (Div. 1) B. Cow Program(dp + 记忆化)

    题目链接:http://codeforces.com/contest/283/problem/B 思路: dp[now][flag]表示现在在位置now,flag表示是接下来要做的步骤,然后根据题意记 ...

  5. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  6. 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] ...

  7. Codeforces Round #349 (Div. 1) B. World Tour 暴力最短路

    B. World Tour 题目连接: http://www.codeforces.com/contest/666/problem/B Description A famous sculptor Ci ...

  8. Codeforces Round #349 (Div. 1)E. Forensic Examination

    题意:给一个初始串s,和m个模式串,q次查询每次问你第l到第r个模式串中包含\(s_l-s_r\)子串的最大数量是多少 题解:把初始串和模式串用分隔符间隔然后建sam,我们需要找到在sam中表示\(s ...

  9. Codeforces Round #349 (Div. 2)

    第一题直接算就行了为了追求手速忘了输出yes导致wa了一发... 第二题技巧题,直接sort,然后把最大的和其他的相减就是构成一条直线,为了满足条件就+1 #include<map> #i ...

随机推荐

  1. 零基础Android学习笔记-01 安卓开发环境搭建

    安卓开发环境搭建. 1.首先准备JDK,从官网找到JDK下载地址,原来做.NET不熟悉JAVA,干脆用最新的,下载了JDK 1.7的版本.原来装过1.5还要配置环境变量什么的.但1.7好像很给力,装好 ...

  2. 关于server的一些小记

    一. 批量创建用户 1. Import-Module ActiveDirectory 2. import-csv e:\users\newusers.csv | 3. New-ADUser -path ...

  3. [老老实实学WCF] 第六篇 元数据交换

    老老实实学WCF 第六篇 元数据交换 通过前两篇的学习,我们了解了WCF通信的一些基本原理,我们知道,WCF服务端和客户端通过共享元数据(包括服务协定.服务器终结点信息)在两个 终结点上建立通道从而进 ...

  4. Cocos2d-JS加速度计与加速度事件

    在很多移动设备的游戏使用到了加速度计,Cocos2d-JS引擎提供了访问加速度计传感器的能力.本节我们首先介绍一下加速度计传感器,然后再介绍如何在Cocos2d-JS中访问加速度计.加速度计加速度计是 ...

  5. iOS-RunLoop,为手机省电,节省CPU资源,程序离不开的机制

    RunLoop是什么?基本操作是什么? 1.RunLoop的作用 RunLoop可以: 保持程序的持续运行 处理App中的各种事件(比如触摸事件.定时器事件.Selector事件) 节省CPU资源,提 ...

  6. 嵌入值和序列化LOB

    Embedded Value 把一个对象映射成另一个对象表中的若干字段. OO系统中会有很多小对象(DataRange,Money).而作为表在DB中毫无意义. 默认想法是把一个对象保存为一个表. 但 ...

  7. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m

    给定两个数m,n,其中m是一个素数. 将n(0<=n<=10000)的阶乘分解质因数,求其中有多少个m. 输入 第一行是一个整数s(0<s<=100),表示测试数据的组数 随后 ...

  8. Codevs 1014 装箱问题

    题目描述 Description 有一个箱子容量为V(正整数,0<=V<=20000),同时有n个物品(0<n<=30),每个物品有一个体积(正整数). 要求n个物品中,任取若 ...

  9. silverlight Frame嵌套页面刷新问题

    1.此方法将会刷新到主页面 private void btn_Click(object sender, RoutedEventArgs e) { HtmlPage.Window.Eval(" ...

  10. php保存base64数据

    php保存base64数据 if(isset($param['cover_pic']) && !empty($param['cover_pic'])) {
 if (preg_matc ...