A. Reberland Linguistics

题目连接:

http://www.codeforces.com/contest/666/problem/A

Description

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

Input

The only line contains a string s (5 ≤ |s| ≤ 104) consisting of lowercase English letters.

Output

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.

Sample Input

abacabaca

Sample Output

3

aca

ba

ca

Hint

题意

给一个字符串,然后你你需要切一个长度至少为5的前缀下来,然后剩下的都得切成是长度为2或者3的字符串

你需要连续的切出来的字符串都不一样,问你能够切出多少不同的块

题解:

前面那个直接n-5就好了,就把前缀切下来了

然后考虑dp,dp[i][0]表示第i个位置,切下长度为2的可不可行

dp[i][1]表示第i个位置,切下长度为3的可不可行

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

dp[i][1]这个转移同理

然后莽一波

代码

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 1e4+6;
char str[maxn];
int dp[maxn][2];
int n;
vector<string>ans;
int main()
{
scanf("%s",str);
n=strlen(str);
reverse(str,str+n);
for(int i=1;i<n-5;i++)
{
if(i==1)
{
string s1="";
s1+=str[i];
s1+=str[i-1];
ans.push_back(s1);
dp[i][0]=1;
}
if(i==2)
{
string s1="";
s1+=str[i];
s1+=str[i-1];
s1+=str[i-2];
ans.push_back(s1);
dp[i][1]=1;
}
if(i-3>=0&&(str[i]!=str[i-2]||str[i-1]!=str[i-3])&&dp[i-2][0]==1)
{
string s1="";
s1+=str[i];
s1+=str[i-1];
ans.push_back(s1);
dp[i][0]=1;
}
if(i-2>=0&&dp[i-2][1]==1)
{
string s1="";
s1+=str[i];
s1+=str[i-1];
ans.push_back(s1);
dp[i][0]=1;
}
if(i-5>=0&&(str[i]!=str[i-3]||str[i-1]!=str[i-4]||str[i-2]!=str[i-5])&&dp[i-3][1]==1)
{
string s1="";
s1+=str[i];
s1+=str[i-1];
s1+=str[i-2];
ans.push_back(s1);
dp[i][1]=1;
}
if(i-3>=0&&dp[i-3][0]==1)
{
string s1="";
s1+=str[i];
s1+=str[i-1];
s1+=str[i-2];
ans.push_back(s1);
dp[i][1]=1;
}
}
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;
}

Codeforces Round #349 (Div. 1) A. Reberland Linguistics 动态规划的更多相关文章

  1. Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接: 题目 A. Reberland Linguistics time limit per test:1 second memory limit per test:256 megabytes 问 ...

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

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

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

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

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

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

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

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

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

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

  8. Codeforces Round #349 (Div. 2)

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

  9. Codeforces Round #349 (Div. 2) D. World Tour 暴力最短路

    D. World Tour   A famous sculptor Cicasso goes to a world tour! Well, it is not actually a world-wid ...

随机推荐

  1. 使用 script 命令记录用户操作行为

    Script 命令可以帮助管理员记录用户的操作行为,包括用户查看文件中的哪些具体内容,写入了哪些文件,写了些什么都能看到,比较详细的记录了用户的操作行为. 本文对此进行简要说明. 1.添加日志记录 e ...

  2. java 一个函数如何返回多个值

    在开发过程中,经常会有这种情况,就是一个函数需要返回多个值,这是一个问题!! 网上这个问题的解决方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值的key是什么,只能通过doc或者通过 ...

  3. wordcount在本地运行报错解决:Exception in thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.io.native.NativeID$Windows.access

    在windows中的intellij中运行wordcount程序,控制台输出以下报错 在Intellij编辑器中解决办法:本地重新创建NativeIO类,修改一个方法返回值,然后用新建的NativeI ...

  4. Sublime Text 3 注册码失效(被移除)解决方法

    最近Sublime Text 3 增加了注册码验证功能,如果你使用共享版本的注册码,可能会提示注册码失效,但是却可以正常激活. 只需要把下面的字段加入到你的hosts文件即可: 127.0.0.1 l ...

  5. 16 Go Concurrency Patterns: Timing out, moving on GO并发模式: 超时, 继续前进

    Go Concurrency Patterns: Timing out, moving on  GO并发模式: 超时, 继续前进 23 September 2010 Concurrent progra ...

  6. css3在动画完成后执行事件

    第一种方法: 用计时器,设定一个和动画时长一样的time,过time事件去执行这个函数. setTimeout(function(){ },time); 第二种方法: 当-webkit-animati ...

  7. 关于int *a; int &a;a; int &a; *a; int * &a

    int i; int*a =&i;//这里a是一个指针,它指向变量i int&b = i;//这里b是一个引用,它是变量i的引用,引用是什么?它的本质是什么?下面会具体讲述 int*& ...

  8. Linux基础入门学习笔记之二

    第三节 用户及文件权限管理 Linux用户管理 Linux是可以实现多用户登录的操作系统 查看用户 who命令用于查看用户 shiyanlou是当前登录用户的用户名 pts/0中pts表示伪终端,后面 ...

  9. 1、树莓派3B开箱+安装系统

    说白了,树莓派就是英国人为学生开发的一款微型电脑.电脑能干什么,那就多了.英国小学生有用树莓派做气象站的,有检测家长开门回家的(可以安心玩游戏了),总之脑洞有多大就可以玩多大. 了解到了之后就一直心水 ...

  10. 一步一步学习IdentityServer4 (2) 开始一个简单的事例

    前面将来一些配置,但是很多都不是必要的,先放一些事例需要的简要配置把 既然是IdentityServer4 所里下面的例子我 直接放到 Linux上 测试环境 CentOS 7 +Nginx1.9.3 ...