洛谷 P1032 字串变换 题解
2024-08-22 12:38:48
每日一题 day19 打卡
Analysis
广搜+map判重
用find寻找字串,再用replace替换字串
这里的map相当于正常广搜的一个book的作用
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#define int long long
#define maxn 6+10
using namespace std;
inline void write(int x)
{
if(x<){putchar('-');x=-x;}
if(x>)write(x/);
putchar(x%+'');
}
string a,b;
string ca[maxn],cb[maxn];
map<string,int> m;
int len=;
queue<string> cq;
queue<int> sq;
inline int bfs()
{
while(!cq.empty()&&cq.front()!=b&&sq.front()<=)
{
if(m[cq.front()]==)
{
cq.pop();
sq.pop();
continue;
}
m[cq.front()]=;
for(int i=;i<=len;i++)
{
string ns=cq.front();
while()
{
int site=ns.find(ca[i]);
if(site==-) break;
string ss=cq.front();
ss.replace(site,ca[i].size(),cb[i]);
cq.push(ss);
sq.push(sq.front()+);
ns[site]='#';
}
}
}
if(cq.empty()||sq.front()>) return -;
else return sq.front();
}
signed main()
{
cin>>a>>b;
while(cin>>ca[len]>>cb[len]) len++;
len--;
if(len==&&a!=b)
{
cout<<"NO ANSWER!";
return ;
}
cq.push(a);
sq.push();
int ans=bfs();
if(ans==-) cout<<"NO ANSWER!";
else write(ans);
return ;
}
请各位大佬斧正(反正我不认识斧正是什么意思)
最新文章
- js 调试
- Premier使用笔记
- 【hadoop】——修改hadoop FileUtil.java,解决权限检查的问题
- 4、面向对象以及winform的简单运用(继承与多态、命名空间与类库)
- 屏蔽wordpress升级提示
- Linux系统内核制作和内核模块的基础
- Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览
- models中的pk主键用法
- Chrome浏览器,处理input自动填充时带黄色背景色
- odoo8 元素简介
- react写一个todo
- 如何构造树状 JSON 数据 JSON-Tree
- Springboot Thymeleaf 发邮件 将html内容展示在邮件内容中
- Java对象的克隆
- POJ-1887 Testing the CATCHER(dp,最长下降子序列)
- vue.js学习 自定义过滤器使用(2)
- ‘,’之后要留空格,如 Function(x, y, z)
- 论文笔记:空间变换网络(Spatial Transformer Networks)
- 022——VUE中remove()方法的使用:
- python3安装 feedparser