题目链接

https://www.luogu.org/problemnew/show/P1032

分析

这题本来很裸的一个BFS,发现其中的字符串操作好烦啊。然后就翻大佬题解发现用STL中的string居然变得这么简洁!!!

各种string操作请看另一位大佬博客,写得很全啊:

https://www.cnblogs.com/rvalue/p/7327293.html#commentform

其实我们这题只用到两个相关函数:\(S.find(string,pos)\)和\(S.substr()\)

前一个是朴素查找,后一个是子串替换,用法都在那个大佬博客中有

代码

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <queue>
#include <iostream>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
#define ll long long
#define ri register int
#define mkp make_pair
using namespace std;
using namespace __gnu_pbds;
template <class T>inline void read(T &x){
x=0;int ne=0;char c;
while(!isdigit(c=getchar()))ne=c=='-';
x=c-48;
while(isdigit(c=getchar()))x=(x<<3)+(x<<1)+c-48;
x=ne?-x:x;return ;
}
const int maxn=25;
const int inf=0x7fffffff;
pair<string,string> pi[maxn],TMP;
int ans=0,cnt=0;
gp_hash_table <string,bool> g;
string A,B;
struct Dat{
string p;
int step;
Dat(){;}
Dat(string _p,int _s){p=_p;step=_s;}
}Tmp;
queue< Dat > q;
int main(){
string a,b;
int t,pos;
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>A>>B;
while(cin>>a>>b){
pi[++cnt]=mkp(a,b);
}
q.push(Dat(A,0));
g[A]=1;
while(q.size()){
Tmp=q.front();
A=Tmp.p,t=Tmp.step;q.pop();
if(A==B){
ans=t;
if(t<10){printf("%d\n",t);}
else puts("NO ANSWER");
return 0;//break;
}
for(ri i=1;i<=cnt;i++){
a=pi[i].first;
pos=A.find(a);//返回查找串开头位置
while(pos!=A.npos){
b=A.substr(0,pos);//将前部分串复制下来
b+=pi[i].second;//拼接串
b+=A.substr(pos+a.size());//将后面的串接上去
if(!g[b]){
q.push(Dat(b,t+1));
g[b]=1;
}
pos=A.find(a,pos+1);
}
}
}
puts("NO ANSWER");
return 0;
}

luogu题解P1032字串变换--BFS+STL:string骚操作的更多相关文章

  1. [luogu普及] ---P1032 字串变换

    目的 刷完100AC (最近很不舒服,写博客耗时啊 记录第一个字符串的题目 参考 https://www.luogu.org/blog/user20197/solution-p1032 代码 #inc ...

  2. 洛谷 P1032 字串变换题解

    题目链接:https://www.luogu.org/problem/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A ...

  3. luogu P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  4. P1032 字串变换 字符串BFS

    题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1​ ->B_1B1​ A_2A2​ -> B_2B2​ 规则的含义为:在 AA中的子串 A_1A1​ ...

  5. [洛谷P1032] 字串变换

    洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...

  6. [NOIP2002] 提高组P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  7. NOIP2002字串变换[BFS]

    题目描述 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ 可以变换为 B1$.A2 ...

  8. 洛谷 P1032 字串变换

    题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...

  9. 字串变换 bfs + 字符串

    题目描述 已知有两个字串A,BA,BA,B及一组字串变换的规则(至多666个规则): A1A_1A1​ ->B1 B_1B1​ A2A_2A2​ -> B2B_2B2​ 规则的含义为:在 ...

随机推荐

  1. mac mysql重置root用户密码

    苹果机安装的MySQL后,设置初始密码 引子:.在苹果机上安装的MySQL之后,通过MySQLWorkBench登录本地数据连接,发现没有密码,而在安装MySQL的过程中,是没有设置过密码的其实,刚刚 ...

  2. 前端知识点回顾——Nodejs

    Node.js 事件循环机制 setImmediate()是将事件插入到事件队列尾部,主线程和事件队列的函数执行完成之后立即执行setImmediate指定的回调函数,和setTimeout(fn,0 ...

  3. LC 835. Image Overlap

    Two images A and B are given, represented as binary, square matrices of the same size.  (A binary ma ...

  4. CentOS linux7 磁盘分区

    常用命令 df [选项] [文件] -a  显示全部文件系统 -h 方便阅读方式显示 -l 只显示本地文件系统 -T 显示文件系统类型 fdisk  /dev/sda1

  5. Aria2Gee 教程

    改定履历 Aria2Gee是什么 开始之前 aria2 frp Aria2Gee可能存在的问题 初级教程 插件的安装 运行状态说明 下载测试 进阶教程 下载百度网盘文件 网盘助手的安装 网盘助手的配置 ...

  6. FlexBox布局的重要属性

    /** * Sample React Native App * https://github.com/facebook/react-native * @flow */ import React, { ...

  7. EF中where中日期帅选问题

    结合 SqlFunctions  和 DbFunctions  不能同时用两个DbFunctions queryAble = queryAble.Where(s=> SqlFunctions.D ...

  8. cmake 在mac系统的安装

    CMake是一个比make更高级的跨平台的安装.编译.配置工具,可以用简单的语句来描述所有平台的安装(编译过程).并根据不同平台.不同的编译器,生成相应的Makefile或者project文件.本文主 ...

  9. (转)window.XMLHttpRequest详解(AJAX工作原理)

    转自:http://l.xbest.blog.163.com/blog/static/8640444120100225516963/?fromdm&fromSearch&isFromS ...

  10. Sequelize+MySQL存储emoji表情

    一.原因 mysql的utf8编码的一个字符最多3个字节,但是一个emoji表情为4个字节,所以utf8不支持存储emoji表情.但是utf8的超集utf8mb4一个字符最多能有4字节,所以能支持em ...