Codeforces 667C Reberland Linguistics 记忆化搜索
链接
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 记忆化搜索的更多相关文章
- CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化
Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...
- CodeForces 398B 概率DP 记忆化搜索
题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...
- CodeForces 132C Logo Turtle (记忆化搜索)
Description A lot of people associate Logo programming language with turtle graphics. In this case t ...
- CodeForces 918D MADMAX(博弈+记忆化搜索)
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Codeforces 667C Reberland Linguistics【DFS】
一道卡题意的题. 题目链接: http://codeforces.com/problemset/problem/667/C 题意: 一个串可以看成一个长度大于4的根,加上其后面的若干个相邻(in a ...
- CodeForces 667C Reberland Linguistics
$dp$. 题意中有一个词组:$in$ $a$ $row$,是连续的意思.... 因此这题只要倒着$dp$一下就可以了.$f[i][0]$表示从$i$位置往后割两个能否割,$f[i][1]$表示从$i ...
- Codeforces #564div2 E1(记忆化搜索)
虽然不是正解但毕竟自己做出来了还是记下来吧- 对每个人分别dfs得到其期望,某两维的组合情况有限所以Hash一下避免了MLE. #include <cstdio> #include < ...
- Codeforces Gym 100231G Voracious Steve 记忆化搜索
Voracious Steve 题目连接: http://codeforces.com/gym/100231/attachments Description 有两个人在玩一个游戏 有一个盆子里面有n个 ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
随机推荐
- 浅析Java抽象类和接口的比較
abstract class和interface是Java语言中对于抽象类定义进行支持的两种机制,正是因为这两种机制的存在,才赋予了Java强大的面向对象能力. abstract class和inte ...
- Android 自己定义RecyclerView 实现真正的Gallery效果
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38173061 .本文出自:[张鸿洋的博客] 上一篇博客我使用自己定义Horizo ...
- bzoj4823: [Cqoi2017]老C的方块(最小割)
4823: [Cqoi2017]老C的方块 题目:传送门 题解: 毒瘤题ORZ.... 太菜了看出来是最小割啥边都不会建...狂%大佬强强强 黑白染色?不!是四个色一起染,四层图跑最小割... 很 ...
- zzulioj--1633--Happy Thanksgiving Day - Hateable Name(字符串筛选)
1633: Happy Thanksgiving Day - Hateable Name Time Limit: 1 Sec Memory Limit: 128 MB Submit: 75 ...
- RBAC介绍
基于角色的权限访问控制(Role-Based Access Control)作为传统访问控制(自主访问,强制访问)的有前景的代替受到广泛的关注.在RBAC中,权限与角色相关联,用户通过成为适当角色的成 ...
- chrome的全局搜索快捷键
常用 文件内搜索 ctrl+f 使用快捷键 ctrl+shift+f, 就会进入到全局搜索模式 打开文件:ctrl+o 打开DevTools 你可以通过以下任何一种方式来访问DevTools: 打开浏 ...
- sql-查看执行计划的方法
sql执行计划:把SQL语句拆分为每个的操作步骤组合,按照一定的顺序执行得出结果,查看并看懂执行计划是调优的关键步骤 查看执行计划的方法 DBMS_XPLAN包 sql*plus AUTO trace ...
- Gram矩阵 迁移学习 one-shot 之类
格拉姆矩阵是由内积空间中的向量两两内积而得.格拉姆矩阵在向量为随机的情况下也是协方差矩阵.每个数字都来自于一个特定滤波器在特定位置的卷积,因此每个数字代表一个特征的强度,而Gram计算的实际上是两两特 ...
- POJ 3617 Best Cow Line 贪心算法
Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 26670 Accepted: 7226 De ...
- SpringCloud学习笔记(16)----Spring Cloud Netflix之Hystrix Dashboard+Turbine集群监控
前言: 上一节中,我们使用Hystrix Dashboard,只能看到单个应用内的服务信息.在生产环境中,我们经常是集群状态,所以我们需要用到Turbine这一应用. 作用:汇总系统内的多个服务的数据 ...