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和 ...
随机推荐
- 【BIEE】03_BIEE数据源配置
声明:此时说的是Oracle数据源配置 BIEE数据源配置有两种方法 ①直接使用字符串连接 ②将tnsnames.ora文件覆盖到obiee目录下 直接使用字符串 直接使用字符串连接很简单 首先打开资 ...
- 用Python实现邮件发送Hive明细数据
代码地址如下:http://www.demodashi.com/demo/12673.html 一.需求描述 客户需要每周周一接收特定的活动数据,生成Excel或是CSV文件,并通过邮件发送给指定接收 ...
- matlab-非线性方程求根函数及函数曲线绘制
Matlab中提供了很多求解非线性方程(y=f(x))的函数,刚開始使用,真的很困惑.全部.这里依据matlab的help文档对这些函数做一些小小的总结 fsolve函数 用来求解非线性方程组:F(x ...
- Qemu事件处理机制简介
Qmeu 采用了基于事件驱动的架构,所有的事件都在一个事件循环(event loop)中被处理,系统中默认的事件循环是在main-loop.c 中的主循环(main loop).我们也可以使用 –ob ...
- 数据库操作事物的四大特性以及MySQL数据库的四种隔离级别
1 .事物操作数据库的四大特性(ACID) 1.原子性 (Atomicity) 原子性:就是事物的所包含的所有操作,要么全部成功,要么全部失败回滚. 2.一致性 (Consistency) 一致性:简 ...
- 通过google地图的webservice根据城市名称获取经纬度
谷歌Geocoding webservice接口获取经纬度信息,由于获取地点的数量级太大,2000多条记录,从response的xml格式中取出该地点的经纬度信息.google有访问限制,如果超出25 ...
- 刨根问底 HTTP 和 WebSocket 协议(上)
HTTP vs WebSocket 那天和boss聊天,不经意间提到了Meteor,然后聊到了WebSocket,然后就有了以下对话,不得不说,看问题的方式不同,看到的东西也会大不相同. A:Mete ...
- 三层登录实例VB.NET版具体解释---理论加实战篇
层,百度百科这样解释,首先-重叠起来的东西:重叠起来的东西中的一部分:层次|表层|大气层.其次-重叠.反复:层峦叠嶂|层出不穷.最后-量词,用于能够分出层次的事物.女孩儿强烈的第六感,三层中的层一定是 ...
- angular选择器功能
1.$event对象 $event对象其实就是潜在的jQuery事件对象,通过$event.currentTarget获取当前元素,通过$event.target获取当前元素的子元素. 例如: ...
- Java常用代码工具类相关
1.HttpServletRequest转换成Map public static Map<String,String> parseXML(HttpServletRequest reques ...