传送门:点击打开链接

题意:给一个串s(|s| < 1e4),这个串由词根和后缀组成,词根的长度 > 4,后缀的长度为2或3。要求同一个后缀不会连续出现2次。问有多少后缀。

思路:我们直接考虑从后往前搜索,保存后一个词和当前的词。但是这样直接搜索肯定会超时的。不过我们可以发现,中间有大量重复的,所以我们只需要记忆化一下位置,当前词的长度,后一个词的长度,就不会超时了

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <bitset>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define fuck(x) cout<<"["<<x<<"]";
#define FIN freopen("input.txt","r",stdin);
#define FOUT freopen("output.txt","w+",stdout);
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const int MX = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;

char S[MX];
int n;

set<string> ans;
bool vis[5][5][MX];
void DFS(int p, int l, string suf, string last) {
    if(l == 4 || p == 5) return;
    string now = S[p] + suf;
    if(l == 1) {
        DFS(p - 1, l + 1, now, last);
        return;
    }

    if(vis[suf.size()][last.size()][p]) return;
    vis[suf.size()][last.size()][p] = 1;

    if(l == 2) DFS(p - 1, l + 1, now, last);
    if(now != last) {
        ans.insert(now);
        DFS(p - 1, 1, "", now);
    }
}

int main() {
    //FIN;
    scanf("%s", S + 1);
    n = strlen(S + 1);
    DFS(n, 1, "", "");

    int sz = ans.size();
    printf("%d\n", sz);
    set<string>::iterator it;
    for(it = ans.begin(); it != ans.end(); it++) {
        printf("%s\n", it->c_str());
    }
    return 0;
}