组队赛三 dfs搜索


先要牢记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";
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值