zoukankan      html  css  js  c++  java
  • Codeforces Round #349 (Div. 1) A. Reberland Linguistics dp

    题目链接:

    题目

    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;
    }
  • 相关阅读:
    中文字体在CSS中的表达方式
    图片上传+预览+剪切解决方案我们到底能走多远系列(20)
    C# — 饼形图之插入介绍文字
    CSS 网页布局中文排版的9则技巧
    Android UI 优化 使用<include/>和 <merge />标签
    SQLite 的日期时间函数
    GSM、GPRS、EDGE、2G、3G与WAP的关系
    WPF中的Style(风格,样式)
    给力分享新的ORM => Dapper
    WCF开发框架形成之旅如何实现X509证书加密
  • 原文地址:https://www.cnblogs.com/fenice/p/5661368.html
Copyright © 2011-2022 走看看