C. Reberland Linguistics(判定性dp+边界)

https://codeforces.com/contest/667/problem/C


思路:

从后往前枚举当前的决策可以转移,从2和3的长度选一个转移。

dp[i][2]/[3]表示以i为起点,长度为2/3是否可以满足,转移从dp[i+x][2/3]找一个满足态。

最后答案扔进set/vector去重

自己写的比较冗余,学习了一下雨雨的(在下面)

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e4+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
LL dp[maxn][4];
vector<string>v;
string get(LL l,LL r){
    string s="";
    for(LL i=l;i<=r;i++) s+=str[i];
    return s;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>(str+1);
  LL len=strlen(str+1);
  if(len<=6){
    cout<<"0"<<"\n";
  }
  else{
    LL ans=0;
    if(len>=7){
        string s=get(len-1,len);
        v.push_back(s);
        dp[len-1][2]=1;
    }
    if(len>=8){
        string s=get(len-2,len);
        v.push_back(s);
        dp[len-2][3]=1;
    }
    if(len-3>5){
        for(LL i=len-3;i<=len-3;i++){
            string s1=get(i,i+1);
            string s2=get(i+2,i+3);
            if(s1!=s2){
                v.push_back(s1);
                dp[i][2]=1;
            }
        }
    }
    for(LL i=len-4;i>5;i--){
            if(dp[i+2][3]){
               dp[i][2]=1;
               string s1=get(i,i+1);
               v.push_back(s1);
            }
            else if(dp[i+2][2]){
                string s1=get(i,i+1);
                string s2=get(i+2,i+3);
                if(s1!=s2){
                    v.push_back(s1);
                    dp[i][2]=1;
                }
            }
            if(dp[i+3][2]){
                string s1=get(i,i+2);
                v.push_back(s1);
                dp[i][3]=1;
            }
            else if(dp[i+3][3]){
                string s1=get(i,i+2);
                string s2=get(i+3,i+5);
                if(s1!=s2){
                    v.push_back(s1);
                    dp[i][3]=1;
                }
            }
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    cout<<v.size()<<"\n";
    for(auto i:v){
        cout<<i<<"\n";
    }
  }
return 0;
}

把提前判的边界初始放到一个循环里,同时把能转移的if也写进来

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e4+100;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
char str[maxn];
LL dp[maxn][4];
string get(LL l,LL r){
    string s="";
    for(LL i=l;i<=r;i++) s+=str[i];
    return s;
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  cin>>(str+1);
  LL len=strlen(str+1);
  if(len<=6){
    cout<<"0"<<"\n";
  }
  else{
    for(LL i=len;i>5;i--){
        if(i+1==len){
            dp[i][2]=1;
            continue;
        }
        if(i+2==len){
            dp[i][3]=1;
            continue;
        }
        if(i+2<len){
            if(dp[i+2][3]==1) dp[i][2]=1;
            else if(dp[i+2][2]==1&&(str[i]!=str[i+2]||str[i+1]!=str[i+3])){
                dp[i][2]=1;
            }
        }
        if(i+3<len){
            if(dp[i+3][2]==1) dp[i][3]=1;
            else if(dp[i+3][3]==1&&(str[i]!=str[i+3]||str[i+1]!=str[i+4]||str[i+2]!=str[i+5])){
                dp[i][3]=1;
            }
        }
    }
    vector<string>v;
    for(LL i=len;i>5;i--){
        if(dp[i][2]==1) v.push_back(get(i,i+1));
        if(dp[i][3]==1) v.push_back(get(i,i+2));
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    cout<<v.size()<<"\n";
    for(auto i:v){
        cout<<i<<"\n";
    }
  }
return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值