UVALive6257-剪枝|dp-Chemist's vows

https://vjudge.net/problem/UVALive-6257
给定一个元素周期表。
问一串字母能否由元素周期表中的元素组成。
打那个表打了好久。
开始写了一个暴力搜索,tle。
后来发现,如果有相邻的两个位置都不能匹配,那么一定是false(我认为这个题之所以tle肯定是因为如果不能匹配,那么要搜索一个满树。),剪枝a了
后来百度了其他写法,有用延迟标记的(我开始也是这样写没写出来qwq)。
当出现两位数的 元素匹配时,再最后的位置标记为true。
出现一位,也标记为true。
并且发现,如果一个连续的两个元素都为true,应该能产生所有的状态。
如果这样还不行,就一定是false。(延迟标记最后位置。。。)
还有一种dp的写法。也挺好的。

#include <bits/stdc++.h>
using namespace std;
map<string,bool>mp;
string  s;
bool vis[50006];
bool flag;
bool  dfs(int loc)
{   if(flag) return false;
    if(loc==s.length()) return true;
    if(loc>s.length()) {
         return false;
       }
       string aa=s.substr(loc,1);
       if(mp[aa])
         if(dfs(loc+1))return true;
        string bb=s.substr(loc,2);
         if(mp[bb])
        if(dfs(loc+2))return true;
        vis[loc]=true;
        if(vis[loc-1]&&vis[loc]){
             flag=true;
        }
        return false;
}
int main()
{   ios::sync_with_stdio(false);
    mp["h"]=true;
     mp["li"]=true;
     mp["be"]=true;
     mp["na"]=true;
     mp["mg"]=true;
     mp["he"]=true;
     mp["b"]=true;
     mp["c"]=true;
     mp["n"]=true;
      mp["o"]=true;

     mp["f"]=true;
     mp["ne"]=true;
     mp["al"]=true;
     mp["si"]=true;
     mp["p"]=true;
     mp["s"]=true;
     mp["cl"]=true;
     mp["ar"]=true;
     mp["k"]=true;
     mp["ca"]=true;
     mp["sc"]=true;
     mp["ti"]=true;
     mp["v"]=true;
     mp["cr"]=true;
     mp["mn"]=true;
      mp["fe"]=true;
     mp["co"]=true;
     mp["ni"]=true;
     mp["cu"]=true;
     mp["zn"]=true;
     mp["ga"]=true;
     mp["ge"]=true;
     mp["as"]=true;
     mp["se"]=true;
     mp["br"]=true;
     mp["kr"]=true;
     mp["rb"]=true;
     mp["sr"]=true;
     mp["y"]=true;
     mp["zr"]=true;
     mp["nb"]=true;
     mp["mo"]=true;
     mp["tc"]=true;
     mp["ru"]=true;
     mp["rh"]=true;
     mp["pd"]=true;
      mp["ag"]=true;
       mp["cd"]=true;
        mp["in"]=true;
         mp["sn"]=true;
          mp["sb"]=true;
           mp["te"]=true;
            mp["i"]=true;
             mp["xe"]=true;
              mp["cs"]=true;
               mp["ba"]=true;
                mp["hf"]=true;
                 mp["ta"]=true;
                  mp["w"]=true;
                   mp["re"]=true;
                    mp["os"]=true;
                     mp["ir"]=true;
                      mp["pt"]=true;
                       mp["au"]=true;
                        mp["hg"]=true;
                         mp["tl"]=true;
                          mp["pb"]=true;

                           mp["bi"]=true;
                            mp["po"]=true;
                             mp["at"]=true;
        mp["rn"]=true;
        mp["fr"]=true;
        mp["ra"]=true;
        mp["rf"]=true;
        mp["db"]=true;
        mp["sg"]=true;
        mp["bh"]=true;
        mp["hs"]=true;
        mp["mt"]=true;
        mp["ds"]=true;
        mp["rg"]=true;
        mp["cn"]=true;
        mp["fl"]=true;
        mp["lv"]=true;
        mp["la"]=true;
        mp["ac"]=true;
        mp["ce"]=true;
        mp["th"]=true;
        mp["pr"]=true;
        mp["pa"]=true;
        mp["nd"]=true;
        mp["u"]=true;
        mp["pm"]=true;
        mp["np"]=true;
        mp["sm"]=true;
        mp["pu"]=true;
        mp["eu"]=true;
        mp["am"]=true;
        mp["gd"]=true;
        mp["cm"]=true;
        mp["tb"]=true;
        mp["bk"]=true;
        mp["dy"]=true;
        mp["cf"]=true;
        mp["ho"]=true;
        mp["es"]=true;
        mp["er"]=true;
        mp["fm"]=true;
        mp["tm"]=true;
        mp["md"]=true;
        mp["yb"]=true;
        mp["no"]=true;
        mp["lu"]=true;
        mp["lr"]=true;
      int m;
      cin>>m;
      for(int i=0;i<m;i++){
         cin>>s;
         memset(vis,false,sizeof(vis));
         flag=false;
         if(dfs(0))
            cout<<"YES"<<endl;
         else
            cout<<"NO"<<endl;
      }

    return 0;
}

2 延迟标记

#include <bits/stdc++.h>
using namespace std;
map<string,bool>mp;
string  s;
bool vis[50006];
bool  dfs(int loc)
{     if(loc>=s.length()) {
         return true;
       }
       string bb=s.substr(loc,2);
         if(mp[bb]&&!vis[loc+1]){
          vis[loc+1]=true;
        if(dfs(loc+2))return true;
        }
       string aa=s.substr(loc,1);
       if(mp[aa]&&!vis[loc]){
         vis[loc]=true;
         if(dfs(loc+1))return true;
         }

        return false;
}
int main()
{   ios::sync_with_stdio(false);
    mp["h"]=true;
     mp["li"]=true;
     mp["be"]=true;
     mp["na"]=true;
     mp["mg"]=true;
     mp["he"]=true;
     mp["b"]=true;
     mp["c"]=true;
     mp["n"]=true;
      mp["o"]=true;

     mp["f"]=true;
     mp["ne"]=true;
     mp["al"]=true;
     mp["si"]=true;
     mp["p"]=true;
     mp["s"]=true;
     mp["cl"]=true;
     mp["ar"]=true;
     mp["k"]=true;
     mp["ca"]=true;
     mp["sc"]=true;
     mp["ti"]=true;
     mp["v"]=true;
     mp["cr"]=true;
     mp["mn"]=true;
      mp["fe"]=true;
     mp["co"]=true;
     mp["ni"]=true;
     mp["cu"]=true;
     mp["zn"]=true;
     mp["ga"]=true;
     mp["ge"]=true;
     mp["as"]=true;
     mp["se"]=true;
     mp["br"]=true;
     mp["kr"]=true;
     mp["rb"]=true;
     mp["sr"]=true;
     mp["y"]=true;
     mp["zr"]=true;
     mp["nb"]=true;
     mp["mo"]=true;
     mp["tc"]=true;
     mp["ru"]=true;
     mp["rh"]=true;
     mp["pd"]=true;
      mp["ag"]=true;
       mp["cd"]=true;
        mp["in"]=true;
         mp["sn"]=true;
          mp["sb"]=true;
           mp["te"]=true;
            mp["i"]=true;
             mp["xe"]=true;
              mp["cs"]=true;
               mp["ba"]=true;
                mp["hf"]=true;
                 mp["ta"]=true;
                  mp["w"]=true;
                   mp["re"]=true;
                    mp["os"]=true;
                     mp["ir"]=true;
                      mp["pt"]=true;
                       mp["au"]=true;
                        mp["hg"]=true;
                         mp["tl"]=true;
                          mp["pb"]=true;

                           mp["bi"]=true;
                            mp["po"]=true;
                             mp["at"]=true;
        mp["rn"]=true;
        mp["fr"]=true;
        mp["ra"]=true;
        mp["rf"]=true;
        mp["db"]=true;
        mp["sg"]=true;
        mp["bh"]=true;
        mp["hs"]=true;
        mp["mt"]=true;
        mp["ds"]=true;
        mp["rg"]=true;
        mp["cn"]=true;
        mp["fl"]=true;
        mp["lv"]=true;
        mp["la"]=true;
        mp["ac"]=true;
        mp["ce"]=true;
        mp["th"]=true;
        mp["pr"]=true;
        mp["pa"]=true;
        mp["nd"]=true;
        mp["u"]=true;
        mp["pm"]=true;
        mp["np"]=true;
        mp["sm"]=true;
        mp["pu"]=true;
        mp["eu"]=true;
        mp["am"]=true;
        mp["gd"]=true;
        mp["cm"]=true;
        mp["tb"]=true;
        mp["bk"]=true;
        mp["dy"]=true;
        mp["cf"]=true;
        mp["ho"]=true;
        mp["es"]=true;
        mp["er"]=true;
        mp["fm"]=true;
        mp["tm"]=true;
        mp["md"]=true;
        mp["yb"]=true;
        mp["no"]=true;
        mp["lu"]=true;
        mp["lr"]=true;
      int m;
      cin>>m;
      for(int i=0;i<m;i++){
         cin>>s;
         memset(vis,false,sizeof(vis));
         if(dfs(0))
            puts("YES");
         else
            puts("NO");
      }

    return 0;
}

3 dp

#include <bits/stdc++.h>
using namespace std;
string 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[50006];
char st[50006];
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;
}
char s[50007];
int main()
{   int t;
    scanf("%d",&t);
      init();
     while(t--){
         scanf("%s",s+1);
           memset(dp,0,sizeof(dp));
           dp[0]=1;
           int len=strlen(s+1);
           for(int i=0;i<len;i++){
                 if(dp[i]){
                 if(tag[s[i+1]-'a'])
                    dp[i+1]=true;
                  if(vis[s[i+1]-'a'][s[i+2]-'a'])
                    dp[i+2]=true;
                    }

           }
           if(dp[len])
            cout<<"YES"<<endl;
           else
            cout<<"NO"<<endl;
     }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值