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

C. Reberland Linguistics

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.

Examples

input

abacabaca

output

3acabaca

Note

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+20, M = 1e6+10, mod = 1e9+7, inf = 1e9+1000;
typedef long long ll;

char a[N];
set<string > s;
int f[N][5];//i开始到l是否可行
int main() {
    scanf("%s",a);
    int n = strlen(a);
    a[n] = ‘0‘;
    a[n+1] =‘0‘;
    a[n+2] = ‘0‘;
    if(n<=6) {
        cout<<0<<endl;
        return  0;
    }
    if(n-2>=5) {
        string tmp;
        tmp = tmp + a[n-2]+ a[n-1];
        s.insert(tmp);
        f[n-2][2] = 1;
    }
     if(n-3>=5) {
        string tmp;
        tmp = tmp + a[n-3]+ a[n-2]+ a[n-1];
        s.insert(tmp);
        f[n-3][3] = 1;
    }
    for(int i=n-4;i>=5;i--) {
        string s1 = "",s2 = "";
        s1 = s1 + a[i] + a[i+1];
        s2 = s2 + a[i+2] + a[i+3];
        if((s1!=s2&&f[i+2][2])||f[i+2][3]) {
            f[i][2] = 1;
            s.insert(s1);//cout<<s1<<endl;
        }
        if(i+2<n) {
            s1 = "",s2 = "";
            s1 = s1 + a[i] + a[i+1] + a[i+2];
            s2 = s2 + a[i+3] + a[i+4] + a[i+5];
            if((s1!=s2&&f[i+3][3])||f[i+3][2]) {
                f[i][3] = 1;
                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 0;
}
时间: 2024-08-10 15:01:14

Codeforces Round #349 (Div. 2) C. Reberland Linguistics DP+set的相关文章

Codeforces Round #349 (Div. 2) C. Reberland Linguistics 【DP】

/* *********************************************** Author :Maltub Email :[email protected] Blog :htttp://www.xiang578.com ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #inclu

Codeforces Round #260 (Div. 1) A. Boredom (DP)

题目链接:http://codeforces.com/problemset/problem/455/A A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Alex doesn't like boredom. That's why whenever he gets bored, he comes up with

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #369 (Div. 2) C. Coloring Trees(dp)

题目链接:Codeforces Round #369 (Div. 2) C. Coloring Trees 题意: 有n个树,每个树有一个颜色,如果颜色值为0,表示没有颜色,一共有m个颜色,第j种颜色涂第i棵树需要花费pij,颜色一样且相邻的分为一组 现在要将所有颜色为0的树涂上颜色,使得这些树恰好可以分为k组,问最小的花费 题解: 考虑dp[i][j][k],表示考虑第i棵树涂第j种颜色,当前分为k组的最小花费,然后状态转移看代码,注意的是dp的初始状态 1 #include<bits/std

Codeforces Round #455 (Div. 2) C. Python Indentation dp递推

Codeforces Round #455 (Div. 2) C. Python Indentation 题意:python 里面,给出 n 个 for 循环或陈述语句,'f' 里面必须要有语句.按 python 缩进的方式组合成合法的程序,问有多少种可能方案. tags: dp dp[i][j] 表示第 i 个语句缩进为 j 时的可能方案数, 转移: 1] 如果第 i 个是 'f' , 则第 i+1 个肯定要比第 i 个多缩进一个单位,即 dp[i+1][j] = dp[i][j]. 2]如果

Codeforces Round #349 (Div. 2)

终于又回到熟悉的Round了 数学 A - Pouring Rain 设个未知数,解方程,还好没有hack点 #include <bits/stdc++.h> typedef long long ll; const int N = 1e5 + 5; const double PI = acos (-1.0); int main() { double d, h, v, e; scanf ("%lf%lf%lf%lf", &d, &h, &v, &

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-wide. But not everyone should have the opportunity to see works of sculptor, shouldn't he? Otherwise there will be no any exclusivity. So Cicasso will enti

Codeforces Round #349 (Div. 2) D. World Tour 【spfa+暴力枚举】

/* *********************************************** Author :Maltub Email :[email protected] Blog :htttp://www.xiang578.com ************************************************ */ #include <cstdio> #include <cstring> #include <iostream> #inclu

Codeforces Round #362 (Div. 1) B. Puzzles 树形dp,概率

题目链接: http://codeforces.com/problemset/problem/696/B 题意: 一个树,dfs遍历子树的顺序是随机的.所对应的子树的dfs序也会不同.输出每个节点的dfs序的期望 思路: http://www.cnblogs.com/01world/p/5795498.html 假设四个子节点为A,B,C,D,因为排列等可能,所以A在B前面的概率跟A在B后面的概率相等,C和D对于A而言一样.所以遍历A的时间期望就是( t(B) + t(C) + t(D) )/2