CodeForces 667C Reberland Linguistics

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-6;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
    char c=getchar(); x=0;
    while(!isdigit(c)) c=getchar();
    while(isdigit(c)) {x=x*10+c-‘0‘; c=getchar();}
}

const int maxn=100100;
char s[maxn];
int len,f[maxn][2];
vector<string>ans;
map<string,bool>d;

int main()
{
    memset(s,0,sizeof s);
    scanf("%s",s); len=strlen(s);
    f[len][0]=f[len][1]=1;
    for(int i=len-1;i>4;i--)
    {
        if(len-i>=2)
        {
            if(f[i+2][1]==1)
            {
                char t[5]; memset(t,0,sizeof t);
                t[0]=s[i]; t[1]=s[i+1];

                f[i][0]=1;
                if(d[t]==0) ans.push_back(t),d[t]=1;
            }
            else if(f[i+2][0]==1)
            {
                char t[5],g[5]; memset(t,0,sizeof t); memset(g,0,sizeof g);
                t[0]=s[i]; t[1]=s[i+1]; g[0]=s[i+2]; g[1]=s[i+3];

                if(strcmp(t,g)==0) continue;
                f[i][0]=1;
                if(d[t]==0) ans.push_back(t),d[t]=1;
            }
        }
        if(len-i>=3)
        {
            if(f[i+3][0]==1)
            {
                char t[5]; memset(t,0,sizeof t);
                t[0]=s[i]; t[1]=s[i+1]; t[2]=s[i+2];

                f[i][1]=1;
                if(d[t]==0) ans.push_back(t),d[t]=1;
            }

            else if(f[i+3][1]==1)
            {
                char t[5],g[5]; memset(t,0,sizeof t); memset(g,0,sizeof g);
                t[0]=s[i]; t[1]=s[i+1]; t[2]=s[i+2];
                g[0]=s[i+3]; g[1]=s[i+4]; g[2]=s[i+5];

                if(strcmp(t,g)==0) continue;
                f[i][1]=1;
                if(d[t]==0) ans.push_back(t),d[t]=1;
            }
        }
    }
    sort(ans.begin(),ans.end());
    printf("%d\n",ans.size());
    for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl;
    return 0;
}
时间: 2024-08-07 08:36:33

CodeForces 667C Reberland Linguistics的相关文章

[2016-05-04][codeforces][666A - Reberland Linguistics]

时间:2016-05-04 16:51:34 星期三 题目编号:[2016-05-04][codeforces][666A - Reberland Linguistics] 题目大意:一个单词由长度不少于5的词根和长度为2或3的若干个后缀组成,并且两个相邻的后缀不能一样,给定一个单词,问这个单词总共可以有多少个后缀 分析: 类似dp 的思想,dp[i] 表示第i个位置到字符串结束能否划分成合法后缀, 那么dp[i] = dp[i +t] ∧ ( dp[i + 5] ∨ 子串(i,t) != 子串

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 d

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 667C DP

题意:给你一个字符串,这个字符串的构造方法如下:先选择一个长度大于4的前缀,然后每次向字符串尾部添加一个长度为2或者长度为3的后缀,不能添加连续的相同的后缀,问可能的后缀有哪些?并按字典序输出去. 思路:第一眼感觉要记忆化,设dp[i]表示把前i个字符作为前缀是否有合法方案,那么只有当前长度为2的串和后面的串重复了,长度为3的串和后面的串重复了,并且dp[i + 5]不合法,dp[i]才不合法.为什么呢?考虑这样一个样例: abcdezzzzzzzz, 合法的后缀有zz 和zzz, 我们可以让z

codeforces 的20道C题

A - Warrior and Archer CodeForces - 595C n  偶数  然后n个数字 A B 轮流取一个 A让差变小B让差变大 直到最后2 个   求的是最小剩下的差 最后剩下的 L R  相距 n/2    求一下最小的就行 #include <iostream> #include <cstdio> #include <cmath> #include <map> #include <algorithm> #include

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 718E】E. Matvey&#39;s Birthday

题目大意&链接: http://codeforces.com/problemset/problem/718/E 给一个长为n(n<=100 000)的只包含‘a’~‘h’8个字符的字符串s.两个位置i,j(i!=j)存在一条边,当且仅当|i-j|==1或s[i]==s[j].求这个无向图的直径,以及直径数量. 题解:  命题1:任意位置之间距离不会大于15. 证明:对于任意两个位置i,j之间,其所经过每种字符不会超过2个(因为相同字符会连边),所以i,j经过节点至多为16,也就意味着边数至多

Codeforces 124A - The number of positions

题目链接:http://codeforces.com/problemset/problem/124/A Petr stands in line of n people, but he doesn't know exactly which position he occupies. He can say that there are no less than a people standing in front of him and no more than b people standing b

Codeforces 841D Leha and another game about graph - 差分

Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or  - 1. To pass th