UVALive - 6257 K - Chemist's vows 【DFS】【BFS】【DP】
题目链接
题意
给出化学元素周期表,然后给出一个字符串,判断字符串里面的字符,能不能够完全由元素周期表里面的元素构成
或者换一种说法,将字符串切割成若干个子串,能不能有一种切割的方式,使得切割下来的每一个子串都是元素周期表里面的元素
思路
其实可以发现 元素周期表里面的元素 不是一个字母的,就是两个字母的
其实 不管是搜索还是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】的更多相关文章
- POJ 2923 【01背包+状态压缩/状压DP】
题目链接 Emma and Eric are moving to their new house they bought after returning from their honeymoon. F ...
- BZOJ2806 [Ctsc2012]Cheat 【后缀自动机 + 二分 + 单调队列优化DP】
题目 输入格式 第一行两个整数N,M表示待检查的作文数量,和小强的标准作文库 的行数 接下来M行的01串,表示标准作文库 接下来N行的01串,表示N篇作文 输出格式 N行,每行一个整数,表示这篇作文的 ...
- 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 ...
- 【dfs序+AC自动机+树状数组】BZOJ2434-[Noi2011]阿狸的打字机
[题目大意] 输入一个字符串,其中:(1)a..z:在字符串末尾添加当前字符(2)P:输出当前字符串(3)B:从当前字符串末尾删去一个字符. 给出m组查询,输出第i个输出的字符串在第j个输出的字符串内 ...
- 【BZOJ3003】LED BFS+状压DP
[BZOJ3003]LED Description LED屏是由一个庞大的点阵小灯泡组成的,一开始每个小灯泡都不发光.每一行一共有N个小灯泡,依次标号为1~n.现在给定K个点,要求这K个点发光,其余点 ...
- #12【BZOJ3003】LED BFS+状压DP
题解: 看到区间修改先想一下差分 这题用差分是为了分析问题 现在的问题就变成了 原序列全为0,要使得特定的k个点变为1,每个操作改变x,y+1 然后我们会发现 对于二元组a,b我们要修改它,实际上是在 ...
- BZOJ3594: [Scoi2014]方伯伯的玉米田【二维树状数组优化DP】
Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美. 这排玉米一共有N株,它们的高度参差不齐. 方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感 ...
- 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 ...
- 【bfs分层图 dp】hihocoder#1147 : 时空阵
最短路径树上分层dp的一类套路吧 题目大意 幽香这几天学习了魔法,准备建造一个大型的时空传送阵. 幽香现在可以在幻想乡的n个地点建造一些传送门,如果她建造了从地点a与地点b之间的传送门,那么从a到b和 ...
随机推荐
- 如何在 Linux 下大量屏蔽恶意 IP 地址
很多情况下,你可能需要在Linux下屏蔽IP地址.比如,作为一个终端用户,你可能想要免受间谍软件或者IP追踪的困扰.或者当你在运行P2P软件时.你可能想要过滤反P2P活动的网络链接.如果你是一名系统管 ...
- 解决eclipse中overlaps the location of another project: 'xxxx'
找遍网络发现各种解释,最常见的一种是: new -> android project -> create project from exist source出现如下错误信息:Invalid ...
- [译] ContentEditable 那些好的、坏的和坑
译者注 我的上一篇译文 “[译] 通过 contentEditable 属性创建一个所见即所得的编辑器” 的原文 “Create a WYSIWYG Editor With the contentEd ...
- HTML5 的四个亮点
1.XDM cross-document-messaging 跨文档消息传递. 2.原生拖放功能. 3.新媒体元素 audio.video. 4.历史状态管理.
- [ Laravel 5.6 文档 ]laravel数据库操作分页(自定义分页实现和自定义分页样式)
简介 在其他框架中,分页可能是件非常痛苦的事,Laravel 让这件事变得简单.易于上手.Laravel 的分页器与查询构建器和 Eloquent ORM 集成在一起,并开箱提供方便的.易于使用的.基 ...
- Hadoop相关资料
http://blog.csdn.net/skywalker_only/article/details/40650427
- nginx反向代理做cache配置
前序:请耐性子看完,前面的这个配置可以达到按后缀名进行缓存,但无法被purge.后面的配置可以被purge.具体实施方案按个人情况而定. 需要第三方的ngx_cache_purge模块:wget ht ...
- windows db2 添加用户权限
http://www.csharpwin.com/csharpspace/12086r9069.shtml 在windows上DB2数据库安装的时候会创建一个系统管理员 的账户,默认为DB2ADMIN ...
- 利用asset存储mesh
做mesh导出的时候遇到了这个问题. 最后解决: 存储mesh数据:AssetDatabase.CreateAsset(meshfilter.mesh, "Assets/" + & ...
- linux授权某个用户对某个目录有读写的权限
针对特定的某一个用户设置文件或目录权限,用setfacl. 首先打开文件系统的acl功能,在挂载参数添加cal,再保存退出,比如/home分区: vim /etc/fstab /dev/sda2 /h ...