组队赛三 dfs搜索

来源:互联网 发布:cms视频监控软件下载 编辑:程序博客网 时间:2024/06/04 17:54


先要牢记string的截取部分的函数。。。

#include <iostream>
#include <string>
using namespace std ;
void main()
{
    string s="ABAB";
    cout << s.substr(2) <<endl ; //输出AB
    cout << s.substr(0,2) <<endl ; //同上
    cout << s.substr(1,2) <<endl ; //输出BA
}

我们令咒文只由两种字符 'E' 和 'X' 组成,对于一个咒文 SS 我们有两种变换:

  • 在 SS 的末尾追加字符 'E'。
  • 在 SS 的末尾追加字符 'X',然后翻转整个咒文(翻转之后,刚才加的 'X' 实际上去到了咒文的开头)。

我们给出一个较短的咒文 SS 和一个较长的咒文 TT (我们保证 TT 的长度严格大于 SS 的长度),问能否通过上述变换把 SS 转化为TT 。


分析:自己没有想到是从target串去搜initial串。。。。结果别人都会,我不会,,自己从S搜T,显然会超时。。


#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#include<string>#include<cstring>#include<iomanip>#include<iostream>#include<stack>#include<cmath>#include<map>#include<vector>#define ll long long#define inf 0x3f3f3f3f#define INF 1000000000#define bug1 cout<<"bug1"<<endl;#define bug2 cout<<"bug2"<<endl;#define bug3 cout<<"bug3"<<endl;using namespace std;string a,b;int lena;map<string,int>mp;bool dfs(string tmp){    int lenb=tmp.length();    //cout<<tmp<<endl;    if(lena==lenb)return a==tmp;    //多理解下这里为什么是这样剪枝吧。            if(tmp[lenb-1]=='E'){                string temp=tmp.substr(0,lenb-1);//                //在这个地方自己又错了。。                 //以为substr(first,second)是截取前面到后面的字符串,其实是从first开始,长度为second的字符串                if(dfs(temp))return true;            }            if(tmp[0]=='X'){                    string temp1=tmp.substr(1,lenb-1);                    reverse(temp1.begin(),temp1.end());                    if(dfs(temp1))return true;            }    return false;}int main(){    int t;    scanf("%d",&t);    while(t--){        cin>>a>>b;        cout<<a.substr(0,a.length()-1)<<endl;        cout<<b.substr(0,b.length()-1)<<endl;        mp.clear();        lena=a.length();        if(dfs(b))cout<<"Explosion!\n";        else cout<<"Dekimasen\n";    }}


0 0
原创粉丝点击