链接

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. JBoss AS 7之文件夹结构(The Return Of The King)

    1.2 JBoss As 7体系结构 以下介绍一下JBoss的体系结构,详细的文件夹结构. 假设熟悉曾经JBoss版本号的人,一定会发现JBoss AS 7与之前的JBoss的文件夹结构有了非常大的不 ...

  2. 对Java、C#转学swift的提醒:学习swift首先要突破心理障碍。

    网上非常多都说swift是一门新手友好的语言. 但以我当年从Java转学Ruby的经验,swift对于从Java.C#转来的程序猿实际并不友好.原因就在于原来总有一种错觉:一个语言最重要的就是严谨,而 ...

  3. 开源ETL工具kettle--数据迁移

    背景 因为项目的需求,须要将数据从Oracle迁移到MSSQL,不是简单的数据复制,而是表结构和字段名都不一样.甚至须要处理编码规范不一致的情况,例如以下图所看到的 watermark/2/text/ ...

  4. hdu_4707

    算是水题一道吧,我也没有建树,看别人又用vector,又用bfs,dfs的,对vector不熟,所以就模拟了一下 #include<iostream> #include<string ...

  5. zzulioj--1799--wrz的压岁钱(贪心)

     1799: wrz的压岁钱 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 407  Solved: 71 SubmitStatusWeb Boa ...

  6. git使用(公钥私钥产生--远程库添加公钥--本地库关联远程库-使用)

    原文1:http://www.cnblogs.com/wangmingshun/p/5424767.html 原文2(指令):http://blog.csdn.net/xiaohanluo/artic ...

  7. iframe 高度宽度自适应

    <iframe id="iframeHome" name="iframeHome" src="/Page/NewHome/GongZuoTai. ...

  8. 上传文件时文件类型限制 <input id="File1" type="file" accept=""/>

    在做项目项目中经常需要上传文件,类型也就那几种.虽然在js中加了上传文件类型的限制,但是为了减少因为用户选择不当而造成的反复检验.可以在input标签上加上accept属性.这种限制只是优化了选择文件 ...

  9. spark thrift server configuration

    # MainApplicationProperties # --master yarn --deploy-mode client 下的配置, client 模式表示,driver 是在本地机器上跑的, ...

  10. Android TextView加下划线的几种方式

    如果是在资源文件里: <resources> <</u></string> <string name="app_name">M ...