记忆化搜索 Codeforces666A Reberland Linguistics

传送门:点击打开链接

题意:给一个串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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值