667C - Reberland Linguistics【dp】


C. Reberland Linguistics time limit per test 1 second memory limit per test 256 megabytes input standard input output 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: 667C - Reberland Linguistics【dp】
文章图片
, 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
3 aca ba ca

input
abaca

output
0

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个字符为这个字符串的根,剩下的由2个或3个字符组成的字符串构成并且相同的字符串不能连续出现两次。问最后的字符串的种类个数,并按照字典序输出。
【667C - Reberland Linguistics【dp】】
/* *********************************************** Author: ryc Created Time : 2016-08-07 Sunday File Name: E:\acm\codeforces\677C.cpp Language: c++ Copyright 2016 ryc All Rights Reserved ************************************************ */ #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; const int maxn=100010; typedef long long LL; bool dp[maxn][2]; void work(){ string str; cin>>str; memset(dp,false,sizeof(dp)); int len=str.length(); str+="0"; dp[len-3][1]=true; dp[len-2][0]=true; for(int i=len-4; i>=5; --i){ if((dp[i+2][1]||(dp[i+2][0]&&(str[i]!=str[i+2]||str[i+1]!=str[i+3])))){ dp[i][0]=true; } if(dp[i+3][0]||(dp[i+3][1]&&(str[i]!=str[i+3]||str[i+1]!=str[i+4]||str[i+2]!=str[i+5]))){ dp[i][1]=true; } } setS; for(int i=5; i



    推荐阅读