链接

Codeforces 667C Reberland Linguistics

题意

给你一个字符串,除去前5个字符串后,使剩下的串有长度为2或3的词根组成,相邻的词根不能重复。找到所有的词根

思路

去掉前5个字符,将剩下的串反过来进行记忆化,用vis[last][pos]记录一下当前状态是否做过。last是之前与之相邻的词根。比赛的时候只用了vis[i]错了。

代码

#include <iostream>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cmath>
#include <cstring>
#include <string> #define LL long long
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MAXN 10005
using namespace std; string s, ss;
int len;
vector<string> res;
map<string, bool> mp;
map<pair<string, int>, bool> vis;
int f[MAXN];
void dfs(int pos, string last){
if (vis[make_pair(last, pos)] == true) return;
int cur = -1;
if (pos >= len) return;
for (int i = 0; i < 2; ++i){
if (pos + 2 + i<= len){
string temp = s.substr(pos, 2 + i);
if (!mp[temp]){
res.push_back(temp);
mp[temp] = true;
}
if (temp != last){
dfs(pos + 2 + i, temp);
}
}
}
vis[make_pair(last,pos)] = true;
} int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
cin >> ss;
ss = ss.substr(5);
len = ss.length();
for (int i = len - 1; i >= 0; --i){
s.push_back(ss[i]);
}
dfs(0, "");
int ans = res.size();
printf("%d\n", ans);
for (int i = 0; i < res.size(); ++i){
int len = res[i].length();
for (int j = 0; j < (len >> 1); ++j){
swap(res[i][j], res[i][len - j - 1]);
}
}
sort(res.begin(), res.end());
for (int i = 0; i < res.size(); ++i){
cout << res[i] << endl;
}
}

Codeforces 667C Reberland Linguistics 记忆化搜索的更多相关文章

  1. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  2. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  3. CodeForces 132C Logo Turtle (记忆化搜索)

    Description A lot of people associate Logo programming language with turtle graphics. In this case t ...

  4. CodeForces 918D MADMAX(博弈+记忆化搜索)

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  5. Codeforces 667C Reberland Linguistics【DFS】

    一道卡题意的题. 题目链接: http://codeforces.com/problemset/problem/667/C 题意: 一个串可以看成一个长度大于4的根,加上其后面的若干个相邻(in a ...

  6. CodeForces 667C Reberland Linguistics

    $dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...

  7. Codeforces #564div2 E1(记忆化搜索)

    虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...

  8. Codeforces Gym 100231G Voracious Steve 记忆化搜索

    Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...

  9. Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索

    D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...

随机推荐

  1. 模拟实现Spring IoC功能

    为了加深理解Spring 今天自己写了一个模拟的Spring.... 步骤: 1.利用jdom解析bean.xml(pull,sax也能够,我这里用了jdom) 2.先解析全部的<bean/&g ...

  2. ubuntu16.04通过ipv6进行学术搜索

    https://www.polarxiong.com/archives/%E8%A7%A3%E5%86%B3ubuntu%E4%B8%8Bipv6%E8%BF%9E%E6%8E%A5%E4%B8%80 ...

  3. [jzoj 6084] [GDOI2019模拟2019.3.25] 礼物 [luogu 4916] 魔力环 解题报告(莫比乌斯反演+生成函数)

    题目链接: https://jzoj.net/senior/#main/show/6084 https://www.luogu.org/problemnew/show/P4916 题目: 题解: 注: ...

  4. python 3.x 学习笔记8 (os模块及xml修改)

    1.os模块操作 os.getcwd():                                       # 查看当前所在路径. os.listdir(path):            ...

  5. bioinformaitcs的latex版本参考文献填坑

    最近实验室投bioinfomatics的刊,编辑说要把参考文献的格式改成不带方括号的,而且加点,而且只保留前三作者,之后用et al. 折腾了一下午,终于弄出来了. 首先,导言区需要添加: \make ...

  6. 视图层 view

    视图层是 Django 处理请求的核心代码层,我们大多数 Python 代码都集中在这一层面.它对外接收用户请求,对内调度模型层和模版层,统合数据库和前端,最后根据业务逻辑,将处理好的数据,与前端结合 ...

  7. [agc011e]increasing numbers

    题意: 如果一个十进制非负整数的所有数位从高位到低位是不减的,我们称它为“上升数”,例如1558,11,3,0都是上升数,而10,20170312则不是: 给定整数N,求最小的k使得N能被表示为k个上 ...

  8. 异步调用task

    异步主要用来提升程序性能,会增加系统的开销(新建一个线程去执行异步任务). 可应用于耗时长的操作,比如:访问数据库时(应用程序和数据库不在同一台服务器上).服务之间的调用(服务会分散在不同的服务器上) ...

  9. CF17E Palisection(manacher)

    题意 给出一个长度为N的字符串S,问S中有多少个回文子串对(i,j)使得i,j在S中的位置相交?(N<=2*106) 题解 #include<iostream> #include&l ...

  10. springMVC 定时器配置

    1.在springMVC中加入 xmlns:task="http://www.springframework.org/schema/task" http://www.springf ...