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;
}