题目链接

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4268

题意

给出化学元素周期表,然后给出一个字符串,判断字符串里面的字符,能不能够完全由元素周期表里面的元素构成

或者换一种说法,将字符串切割成若干个子串,能不能有一种切割的方式,使得切割下来的每一个子串都是元素周期表里面的元素

思路

其实可以发现 元素周期表里面的元素 不是一个字母的,就是两个字母的

其实 不管是搜索还是DP 思路都是差不多的

搜索:

void dfs(int index)

表示 从 0 - index - 1 这个字段 是可以满足要求的

那么我们去判断 s[index] 是不是元素周期表的元素 如果是 那么0 - index 也是满足要求的 那么我们继续dfs(index + 1)

如果 s[index] + s[index + 1] 是元素周期表的元素 就dfs(index + 2)

Tips:

很重要的一点 ,一定要记得访问标记啊,不然会T到不知道什么样子。。。

如果这个当前下标的状态是访问过的,就不用再继续搜下去了

DP:

dp[i] 表示 从 0 - 第i 个字符构成的子段是否是满足要求的

那么有两种情况可以转移过来

如果 dp[i - 1] 是满足要求 并且 s[i] 是元素周期表里面的元素 那么 dp[i] 也是满足要求的

如果 dp[i - 2] 满足要求 并且 s[i - 1] 和 s[i] 构成的子段 是元素周期表里面的元素 那么 dp[i] 也是满足要求的

上面两个条件满足其中一个就可以了

AC代码

DP

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
//#define bug
//#define gets gets_s using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int MOD = 1e9 + 7; char single[25][2] = { "h","b","c","n","o","f","k","p","s","y","i","w","u","v" };
char ss[130][3] = { "he","li","be","ne","na","mg",
"al","si","cl","ar","ca","sc","ti","cr","mn",
"fe","co","ni","cu","zn","ga","ge","as","se",
"br","kr","rb","sr","zr","nb","mo","tc","ru",
"rh","pd","ag","cd","in","sn","sb","te","xe",
"cs","ba","hf","ta","re","os","ir","pt","au",
"hg","tl","pb","bi","po","at","rn","fr","ra",
"rf","db","sg","bh","hs","mt","ds","rg","cn",
"fl","lv","la","ce","pr","nd","pm","sm","eu",
"gd","tb","dy","ho","er","tm","yb","lu","ac",
"th","pa","np","pu","am","cm","bk","cf","es",
"fm","md","no","lr" }; int len; char s[maxn];
int dp[maxn]; bool judge_single(int index)
{
for (int i = 0; i < 14; i++)
{
if (single[i][0] == s[index])
return true;
}
return false;
} bool judge_tw(int index)
{
for (int i = 0; i < 100; i++)
{
if (ss[i][0] == s[index] && ss[i][1] == s[index + 1])
return true;
}
return false;
} int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%s", s);
len = strlen(s);
for (int i = 0; i < len; i++)
dp[i] = 0;
if (judge_single(0))
dp[0] = 1;
if ((judge_single(1) && dp[0]) || judge_tw(0))
dp[1] = 1;
for (int i = 2; i < len; i++)
dp[i] = ((judge_single(i) && dp[i - 1]) || (judge_tw(i - 1) && dp[i - 2]));
puts(dp[len - 1] ? "YES" : "NO");
}
}

DFS

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
//#define bug
//#define gets gets_s using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int MOD = 1e9 + 7; char single[20] = //14
{
'h', 'b', 'c', 'n', 'o', 'p', 's', 'v', 'k', 'y', 'i', 'u', 'f', 'w',
}; char ss[105][3]
{
"he", "li", "be", "ne", "na", "mg", "al", "si", "cl", "ar", "ca", "sc", "ti", "cr", "mn", "fe", "co", "ni", "cu", "zn", "ga", "ge", "as", "se", "br", "kr", "rb", "sr", "zr", "nb", "mo", "tc", "ru", "rh", "pd", "ag", "cd", "in", "sn", "sb", "te", "xe", "cs", "ba", "hf", "ta", "re", "os", "ir", "pt", "au", "hg", "tl", "pb", "bi", "po", "at", "rn", "fr", "ra", "rf", "db", "sg", "bh", "hs", "mt", "ds", "rg", "cn", "fl", "lv", "la", "ce", "pr", "nd", "pm", "sm", "eu", "gd", "tb", "dy", "ho", "er", "tm", "yb", "lu", "ac", "th", "pa", "np", "pu", "am", "cm", "bk", "cf", "es", "fm", "md", "no", "lr",
}; int len; char s[maxn]; bool judge_single(int index)
{
for (int i = 0; i < 14; i++)
{
if (single[i] == s[index])
return true;
}
return false;
} bool judge_tw(int index)
{
for (int i = 0; i < 100; i++)
{
if (ss[i][0] == s[index] && ss[i][1] == s[index + 1])
return true;
}
return false;
} int tag; int used[maxn]; void dfs(int index)
{
used[index] = 1;
if (index == len)
{
tag = 1;
return;
}
if (judge_single(index) && !used[index + 1])
dfs(index + 1);
if (index + 1 < len && judge_tw(index) && !used[index + 2])
dfs(index + 2);
} int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%s", s);
len = strlen(s);
for (int i = 0; i <= len; i++)
used[i] = 0;
tag = 0;
dfs(0);
puts(tag ? "YES" : "NO");
}
}

BFS

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <list>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a, b) memset(a, (b), sizeof(a))
#define pb push_back
#define bug puts("***bug***");
#define fi first
#define se second
#define stack_expand #pragma comment(linker, "/STACK:102400000,102400000")
//#define bug
//#define gets gets_s using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <string, int> psi;
typedef pair <string, string> pss;
typedef pair <double, int> pdi; const double PI = acos(-1.0);
const double E = exp(1.0);
const double eps = 1e-8; const int INF = 0x3f3f3f3f;
const int maxn = 5e4 + 10;
const int MOD = 1e9 + 7; char single[20] = //14
{
'h', 'b', 'c', 'n', 'o', 'p', 's', 'v', 'k', 'y', 'i', 'u', 'f', 'w',
}; char ss[105][3]
{
"he", "li", "be", "ne", "na", "mg", "al", "si", "cl", "ar", "ca", "sc", "ti", "cr", "mn", "fe", "co", "ni", "cu", "zn", "ga", "ge", "as", "se", "br", "kr", "rb", "sr", "zr", "nb", "mo", "tc", "ru", "rh", "pd", "ag", "cd", "in", "sn", "sb", "te", "xe", "cs", "ba", "hf", "ta", "re", "os", "ir", "pt", "au", "hg", "tl", "pb", "bi", "po", "at", "rn", "fr", "ra", "rf", "db", "sg", "bh", "hs", "mt", "ds", "rg", "cn", "fl", "lv", "la", "ce", "pr", "nd", "pm", "sm", "eu", "gd", "tb", "dy", "ho", "er", "tm", "yb", "lu", "ac", "th", "pa", "np", "pu", "am", "cm", "bk", "cf", "es", "fm", "md", "no", "lr",
}; int len; char s[maxn]; int used[maxn]; bool judge_single(int index)
{
for (int i = 0; i < 14; i++)
{
if (single[i] == s[index])
return true;
}
return false;
} bool judge_tw(int index)
{
for (int i = 0; i < 100; i++)
{
if (ss[i][0] == s[index] && ss[i][1] == s[index + 1])
return true;
}
return false;
} bool bfs()
{
queue <int> q;
if (judge_single(0))
q.push(1), used[1] = 1;
if (1 < len && judge_tw(0))
q.push(2), used[2] = 1;
while (!q.empty())
{
int u = q.front();
q.pop();
if (u == len)
return true;
if (judge_single(u) && !used[u + 1])
q.push(u + 1), used[u + 1] = 1;
if (u + 1 < len && judge_tw(u) && !used[u + 2])
q.push(u + 2), used[u + 2] = 1;
}
return false;
} int tag; int main()
{
int t;
cin >> t;
while (t--)
{
scanf("%s", s);
len = strlen(s);
for (int i = 0; i <= len; i++)
used[i] = 0;
puts(bfs() ? "YES" : "NO");
}
}

UVALive - 6257 K - Chemist's vows 【DFS】【BFS】【DP】的更多相关文章

  1. POJ 2923 【01背包+状态压缩/状压DP】

    题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...

  2. BZOJ2806 [Ctsc2012]Cheat 【后缀自动机 + 二分 + 单调队列优化DP】

    题目 输入格式 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库 的行数 接下来M行的01串,表示标准作文库 接下来N行的01串,表示N篇作文 输出格式 N行,每行一个整数,表示这篇作文的 ...

  3. HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. 【dfs序+AC自动机+树状数组】BZOJ2434-[Noi2011]阿狸的打字机

    [题目大意] 输入一个字符串,其中:(1)a..z:在字符串末尾添加当前字符(2)P:输出当前字符串(3)B:从当前字符串末尾删去一个字符. 给出m组查询,输出第i个输出的字符串在第j个输出的字符串内 ...

  5. 【BZOJ3003】LED BFS+状压DP

    [BZOJ3003]LED Description LED屏是由一个庞大的点阵小灯泡组成的,一开始每个小灯泡都不发光.每一行一共有N个小灯泡,依次标号为1~n.现在给定K个点,要求这K个点发光,其余点 ...

  6. #12【BZOJ3003】LED BFS+状压DP

    题解: 看到区间修改先想一下差分 这题用差分是为了分析问题 现在的问题就变成了 原序列全为0,要使得特定的k个点变为1,每个操作改变x,y+1 然后我们会发现 对于二元组a,b我们要修改它,实际上是在 ...

  7. BZOJ3594: [Scoi2014]方伯伯的玉米田【二维树状数组优化DP】

    Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美. 这排玉米一共有N株,它们的高度参差不齐. 方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感 ...

  8. codeforces Round 442 B Nikita and string【前缀和+暴力枚举分界点/线性DP】

    B. Nikita and string time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. 【bfs分层图 dp】hihocoder#1147 : 时空阵

    最短路径树上分层dp的一类套路吧 题目大意 幽香这几天学习了魔法,准备建造一个大型的时空传送阵. 幽香现在可以在幻想乡的n个地点建造一些传送门,如果她建造了从地点a与地点b之间的传送门,那么从a到b和 ...

随机推荐

  1. 如何检测一个aspx页面的速度慢的原因

    最近读到一篇文章,是关于如何提高一个aspx页面的速度.这是一个常见的面试问题.该问题原文出自这个网站. 出现这个问题的原因会多种多样,我们需要一步一步的排查来定位问题真正出现在哪里. 1. 找出那一 ...

  2. Linux服务器重启后启动Oracle服务

    目录 1. 启动Oracle服务 2. 启动Oracle监听服务 © 版权声明:本文为博主原创文章,转载请注明出处 1. 启动Oracle服务 重启Linux服务器后,Oracle服务还需要手动启动. ...

  3. EularProject 39:给周长推断构成直角三角形个数

    华电北风吹 天津大学认知计算与应用重点实验室 完毕日期:2015/7/30 Integer right triangles Problem 39 If p is the perimeter of a ...

  4. poj3411--Paid Roads(bfs+状压)

    题目链接:id=3411">点击打开链接 题目大意:有n个点.m条有向边,经过边须要一个花费,a b c p q代表 a到b的一条道路,假设经过这条边之前经过c点,那么须要p的花费,否 ...

  5. 《HBase in Action》 第三章节的学习总结 ---- 如何编写和运行基于HBase的MapReduce程序

    HBase之所以与Hadoop是最好的伙伴,我理解就因为两点:1.HADOOP的HDFS,为HBase提供了分布式的存储方式:2.HADOOP的MR为HBase提供的分布式的计算方法.u 其中第一点, ...

  6. 在Windows 10中开启开发者模式

    及以上)的电脑上使用Visual Studio来开发Windows 10或者Windows 8.1的应用,你可能会遇到下面的问题,要求你开启开发者模式. 于是你跑到设置里面,把开发者模式打开: 结果你 ...

  7. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException: Unknown column 'viewpoint' in 'field list'

    问题描述:当我在model中添加了一下代码以后数据库报错: 添加的代码为: private Viewpoint viewpoint; public Viewpoint getViewpoint() { ...

  8. CardView的具体使用方法(转)

    转载自:CardView的具体使用方法  因为学习做此记录方便查找使用 今天主要是CardView的用法,CardView是在安卓5.0提出的卡片式控件.首先介绍一下它的配置. 在gradle文件下添 ...

  9. onInterceptTouchEvent和onTouchEvent调用时序(转)

    onInterceptTouchEvent和onTouchEvent调用时序 onInterceptTouchEvent()是ViewGroup的一个方法,目的是在系统向该ViewGroup及其各个c ...

  10. Linux - SVN的基本操作

    SVN的基本操作 本文地址: http://blog.csdn.net/caroline_wendy $ svn diff //显示改动 $ post-review --summary="b ...