Codeforces 667C Reberland Linguistics 【dp】

来源:互联网 发布:java开发月薪 编辑:程序博客网 时间:2024/06/10 20:35

题目链接:Codeforces 667C Reberland Linguistics

C. Reberland Linguistics
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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}.

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.

题意:给定一个串,每次选择[1, R]的字符串(R >= 5),剩余的串要用长度为2或者3的字符串s完全拼接成功,且不能存在两个串相等。问你所有s串的集合。

思路:考虑dp
dp[i][0]表示处理到第i个字符,截取(i, i + 2)是否可行;
dp[i][1]表示处理到第i个字符,截取(i, i + 3)是否可行;
倒着跑一遍就可以了。

AC代码:

#include <cstdio>#include <cstring>#include <cmath>#include <map>#include <set>#include <iostream>#include <algorithm>#include <string>#define CLR(a, b) memset(a, b, sizeof(a))using namespace std;typedef long long LL;typedef pair<int, int> pii;const int MAXN = 1e5 + 10;const int MOD = 1e9 + 7;const int INF = 0x3f3f3f3f;set<string> ans;set<string> :: iterator it;string str;bool dp[MAXN][2];int main(){    while(cin >> str) {        ans.clear(); CLR(dp, 0); int len = str.size();        for(int i = len-1; i >= 5; --i) {            if(i + 2 == len) {                dp[i][0] = true;                ans.insert(str.substr(i, 2));                continue;            }            if(i + 3 == len) {                dp[i][1] = true;                ans.insert(str.substr(i, 3));                continue;            }            if(dp[i+2][1] || (dp[i+2][0] && str.substr(i, 2) != str.substr(i + 2, 2))) {                dp[i][0] = true;                ans.insert(str.substr(i, 2));            }            if(dp[i+3][0] || (dp[i+3][1] && str.substr(i, 3) != str.substr(i + 3, 3))) {                dp[i][1] = true;                ans.insert(str.substr(i, 3));            }        }        cout << ans.size() << endl;        for(it = ans.begin(); it != ans.end(); ++it) {            cout << *it << endl;        }    }    return 0;}
0 0
原创粉丝点击