UVALive 6257 Chemist's vows --一道题的三种解法(模拟,DFS,DP)

题意:给一个元素周期表的元素符号(114种),再给一个串,问这个串能否有这些元素符号组成(全为小写)。

解法1:动态规划

定义:dp[i]表示到 i 这个字符为止,能否有元素周期表里的符号构成。

则有转移方程:dp[i] = (dp[i-1]&&f(i-1,1)) || (dp[i-2]&&f(i-2,2))     f(i,k):表示从i开始填入k个字符,这k个字符在不在元素周期表中。  dp[0] = 1

代码:

//109ms 0KB
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 50007string single[25] = {"h","b","c","n","o","f","k","p","s","y","i","w","u","v"};
string ss[130] = {"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 vis[30][30],tag[30];
int dp[N];
char st[N];void init()
{memset(vis,0,sizeof(vis));memset(tag,0,sizeof(tag));for(int i=0;i<14;i++)tag[single[i][0]-'a'] = 1;for(int i=0;i<100;i++)vis[ss[i][0]-'a'][ss[i][1]-'a'] = 1;
}int main()
{int t,len,i;init();scanf("%d",&t);while(t--){scanf("%s",st+1);len = strlen(st+1);memset(dp,0,sizeof(dp));dp[0] = 1;for(i=0;i<len;i++){if(dp[i]){if(tag[st[i+1]-'a'])dp[i+1] = 1;dp[i+2] |= vis[st[i+1]-'a'][st[i+2]-'a'];}}if(dp[len])puts("YES");elseputs("NO");}return 0;
}
View Code

 

解法2:DFS

搜索时循环的是元素周期表的符号个数。详见代码

代码: (306ms)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 50007string ss[130] = {"h","b","c","n","o","f","k","p","s","y","i","w","u","v","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 vis[N];
int len[140];
char st[N];
int Length;
bool Tag;void init()
{int i;for(i=0;i<14;i++)len[i] = 1;for(i=14;i<114;i++)len[i] = 2;
}void dfs(int u)
{if(u == Length)Tag = 1;if(Tag)return;for(int i=0;i<114;i++){int flag = 1;if(u+len[i] <= Length && !vis[u+len[i]]){for(int j=0;j<len[i];j++){if(ss[i][j] != st[u+j]){flag = 0;break;}}if(flag){vis[u+len[i]] = 1;dfs(u+len[i]);}}}
}int main()
{init();int t,i;scanf("%d",&t);while(t--){scanf("%s",st);Length = strlen(st);memset(vis,0,sizeof(vis));Tag = 0;dfs(0);if(Tag)puts("YES");elseputs("NO");}return 0;
}
View Code

 

解法3:乱搞,模拟。

分成: 单个元素存在与否,与前面匹不匹配,与后面匹不匹配,总共2^3 = 8种情况,然后O(n)扫过去,代码很长。。。

代码:(586ms)

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
using namespace std;
#define N 50007string single[25] = {"h","b","c","n","o","f","k","p","s","y","i","w","u","v"};
string ss[130] = {"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"};char st[N];
int vis[N];int main()
{int t,len,i,j,k;scanf("%d",&t);while(t--){scanf("%s",st);len = strlen(st);int flag = 1;memset(vis,0,sizeof(vis));for(i=0;i<len;i++){if(vis[i])continue;string S = "";S += st[i];for(j=0;j<14;j++){if(single[j] == S)break;}if(j == 14)  //not single
            {if(i > 0 && !vis[i-1]){S = st[i-1]+S;for(j=0;j<100;j++){if(ss[j] == S)break;}if(j != 100)  //pre match
                    {if(i < len-1){string ks = "";ks += st[i];ks += st[i+1];for(k=0;k<100;k++){if(ss[k] == ks)break;}if(k != 100)  //back matchvis[i] = 0;else   //back not matchvis[i] = 1;}}else   //pre not match
                    {if(i < len-1){string ks = "";ks += st[i];ks += st[i+1];for(k=0;k<100;k++){if(ss[k] == ks)break;}if(k != 100)  //back matchvis[i+1] = 1;else   //back not match
                            {flag = 0;break;}}else{flag = 0;break;}}}else{if(i < len-1){string ks = "";ks += st[i];ks += st[i+1];for(k=0;k<100;k++){if(ss[k] == ks)break;}if(k != 100)  //back matchvis[i+1] = 1;else   //back not match
                        {flag = 0;break;}}else{flag = 0;break;}}}else   //single
            {if(i > 0 && !vis[i-1]){S = st[i-1]+S;for(j=0;j<100;j++){if(ss[j] == S)break;}if(j != 100)  //pre match
                    {if(i < len-1){string ks = "";ks += st[i];ks += st[i+1];for(k=0;k<100;k++){if(ss[k] == ks)break;}if(k != 100)  //back matchvis[i] = 0;else   //back not matchvis[i] = 1;}}else   //pre not match
                    {if(i < len-1){string ks = "";ks += st[i];ks += st[i+1];for(k=0;k<100;k++){if(ss[k] == ks)break;}if(k != 100)  //back matchvis[i] = 0;else   //back not matchvis[i] = 1;}elsevis[i] = 1;}}else{if(i < len-1){string ks = "";ks += st[i];ks += st[i+1];for(k=0;k<100;k++){if(ss[k] == ks)break;}if(k != 100)  //back matchvis[i] = 0;else   //back not matchvis[i] = 1;}elsevis[i] = 1;}}}if(flag)puts("YES");elseputs("NO");}return 0;
}
View Code

 

转载于:https://www.cnblogs.com/whatbeg/p/3876636.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/376792.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

hdu 1025(最长非递减子序列的n*log(n)求法)

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1025 经典题。。。最长非递减序列的n*log(n)求法。。。orz... View Code 1 #include<iostream>2 const int N500007;3 using namespace std;4 int city[N];5 int dp[N];//dp[i]保存的是长度为i的最长不降…

消息队列重要机制讲解以及MQ设计思路(kafka、rabbitmq、rocketmq)

目录《Kafka篇》简述kafka的架构设计原理&#xff08;入口点&#xff09;消息队列有哪些作用&#xff08;简单&#xff09;消息队列的优缺点&#xff0c;使用场景&#xff08;基础&#xff09;消息队列如何保证消息可靠传输死信队列是什么&#xff1f;延时队列是什么&#xff1…

数据库归档模式

1、在sys身份下登陆oracle&#xff0c;执行命令archive log list; SQL> archive log list; Database log mode Archive Mode Automatic archival Enabled Archive destination USE_DB_RECOVERY_FILE_DEST Oldest online log sequence …

转载|网络编程中阻塞式函数的底层逻辑

逛知乎看到的&#xff0c;觉得写的挺透彻的&#xff0c;转载一下&#xff0c;原文链接&#xff1a;Unix网络编程里的阻塞是在操作系统的内核态创建一个线程来死循环吗&#xff1f; 原文以阻塞式的recv函数作为讲解&#xff0c;但是所有阻塞式的api底层逻辑基本相通。 下面是正文…

树的存储结构2 - 数据结构和算法42

树的存储结构 让编程改变世界 Change the world by program 孩子表示法 我们这次换个角度来考虑&#xff0c;由于树中每个结点可能有多棵子树&#xff0c;可以考虑用多重链表来实现。 就像我们虽然有计划生育&#xff0c;但我们还是无法确保每个家庭只养育一个孩子的冲动&a…

Sharepoint 2013 发布功能(Publishing features)

一、默认情况下&#xff0c;在创建网站集时&#xff0c;只有选择的模板为‘ Publishing Portal&#xff08;发布门户&#xff09;’与‘ Enterprise Wiki&#xff08;企业 Wiki&#xff09;’时才默认启用发布功能&#xff0c;如下图所示&#xff1a; 二、发布功能包含两块&…

【草稿】windows + vscode 远程开发

主要分为三个步骤&#xff1a; 1、开启openssh服务 2、通过ssh命令连接到远程服务器 3、通过vscode连接远程服务器进行开发调试 ssh概念 SSH是较可靠&#xff0c;专为远程登陆会话和其他网络服务提供安全性得协议&#xff0c;利用ssh协议可以有效防止远程管理过程中得信息…

POJ3185(简单BFS,主要做测试使用)

没事做水了一道POJ的简单BFS的题目 这道题的数据范围是20,所以状态总数就是&#xff08;1<<20&#xff09; 第一次提交使用STL的queue&#xff0c;并且是在队首判断是否达到终点&#xff0c;达到终点就退出&#xff0c;超时&#xff1a;&#xff08;其实这里我是很不明白…

sql server根据表中数据生成insert语句

几个收藏的根据数据库生成Insert语句的存储过程[修正版]----根据表中数据生成insert语句的存储过程--建立存储过程&#xff0c;执行spGenInsertSQL 表名--感谢playyuer----感谢szyicol--CREATEproc[dbo].[spGenInsertSQL](tablenamevarchar(256))asbegindeclaresqlvarchar(8000…

杂感无题|

今天中午和组里面的人吃饭&#xff0c;聊起了科兴跳楼的事情。这事其实前几天我华为的mentor就转给我了&#xff0c;当时也没太在意&#xff0c;在脉脉上看了看&#xff0c;也不知晓是谁&#xff0c;想着可能又是抑郁症吧。 饭后依旧绕着食堂散步&#xff0c;ly说那个人好像还是…

uva1366_Martian Mining_简单DP

题目不难&#xff0c;却想了好长时间&#xff0c;目测自己DP还是很水。。。囧 思路&#xff1a;舍f[i][j]为前i行j列的最大矿总量不难推出状态转移方程为f[i][j]max(f[i-1][j]line[i][j],f[i][j-1]row[j][i]) 其中line[i][j]为第i行前j个A矿的和&#xff08;a[i][1]a[i][2]...a…

数学图形之Boy surface

这是一个姓Boy的人发现的,所以取名为Boy surface.该图形与罗马图形有点相似,都是三分的图形.它甚至可以说是由罗马曲面变化而成的. 本文将展示几种Boy曲面的生成算法和切图,使用自己定义语法的脚本代码生成数学图形.相关软件参见:数学图形可视化工具,该软件免费开源.QQ交流群: …

开个定时器给echarts组件配置定时更新

我在js文件中开了个定时器&#xff0c;每1s从后端获取数据并解析&#xff0c;然后用异步方法就渲染不出来&#xff0c;改成同步就可以了。 这个解决方法来自于这篇文章&#xff0c;我出的问题和他一样&#xff1a;关于ajax中readyState的值一直为1的问题 这里将ajax参数修改为f…

关于Oracle Insert 语句的子查询 和 with check option的用法

今日睇ocp教程 发现 insert语句还可以子查询例如&#xff1a;INSERT INTO (SELECT employee_id, last_name, email, hire_date, job_id, salary, department_id FROM employees where department_id 50 )VALUES (9999…

ORACLE基本SQL语句-查询篇

一、普通查询 /*查询表数据*/select * from STU /*取出前3行数据*/select * from stu where ROWNUM<3 /*模糊查询*/select * from stu where stu_id like stu001% 说明&#xff1a;通配符“%”代表一个或者多个字符&#xff0c;通配符“_”代表一个字符。 /*别名*/select S…

三次握手建立失败的几种情况以及三次握手的理解

上面的图是阻塞式socket进行通信的过程&#xff0c;阻塞的时候是操作系统内核网络协议栈在工作 调用 connect 函数将激发 TCP 的三次握手过程&#xff0c;而且仅在连接建立成功或出错时才返回。其中出错返回可能有以下几种情况&#xff1a; 1、三次握手无法建立&#xff0c;客…

在熟练使用2B铅笔前,请不要打开Axure

在互联网产品领域&#xff0c;Axure已成为产品经理、产品设计师以及交互设计师的必备工具&#xff0c;从某种程度讲&#xff0c;Axure帮助我们建立低保真模型&#xff0c;便于与用户的需求验证&#xff0c;也帮助我们构思交互细节&#xff0c;使前端和开发人员更容易理解我们的…

启用isqlplus

iSQL*Plus是sqlplus基于web方式发布的&#xff0c;要使用它只要在服务器上开启即可&#xff1a; [oraclelocalhost ~]$ isqlplusctl start perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE (unset), LC_ALL (unset)…

echarts 怎么知道鼠标点击的哪根柱子

有个需求&#xff0c;点击柱子&#xff0c;然后得到该柱子的信息&#xff0c;然后展示这个机房的时序图。 第一步卡住了&#xff0c;就是不知道如何获取柱子的序号。后参考&#xff1a;https://blog.csdn.net/zt_fucker/article/details/72461572?utm_sourceblogxgwz1 得到思路…

codeforces 261 D

题目链接&#xff1a; 解题报告&#xff1a;给出一个序列a1,a2,a3.........an&#xff0c;f(i , j ,x) ak 等于x的个数(i < k < j)&#xff0c;令i < j&#xff0c;求有多少对 i 和 j 使得 f(1,i,ai) > f(j,n,aj)。 从左往右扫一遍这个序列&#xff0c;num1[i] 等于…