题目链接:https://www.luogu.org/problemnew/show/P1032

题意:

给定一个原字符串和目标字符串,以及几个字符串变换的规则。

问能否根据这几个规则在十步之内把原字符串变为目标字符串。

思路:

bfs,队列维护字符串和所经过的步骤这样一个二元组而不是简单的字符串。

每一次取出一个字符串,用所给的规则进行变换得到新的字符串。

由于字符串中可能有多次匹配,所以用vector存每一次匹配的开始位置,这些都是独立的一次变换都要单独存入vector中。【这一点最开始没有考虑到,图方便用了.find()】

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue> using namespace std; string A, B;
struct rule{
string a, b;
}rules[];
struct node{
string s;
int steps;
node(){}
node(string _s, int _steps){
s = _s;
steps = _steps;
}
};
set<string>sset; int main()
{
cin>>A>>B;
int n = ;
while(cin>>rules[n].a>>rules[n].b)n++; queue<node>que;
que.push(node(A, ));
int steps = ;
bool flag = false;
while(!que.empty()){
node now = que.front();que.pop();
//cout<<now.s<<" "<<now.steps<<endl;
if(now.s == B){
flag = true;
steps = now.steps;
break;
}
if(now.steps == )continue;
for(int i = ; i < n; i++){
//cout<<i<<endl;
vector<int>pos;
//cout<<rules[i].a.length()<<endl;
//cout<<now.s.length()<<endl;
//cout<<now.s.length() - rules[i].a.length()<<endl;
for(int j = ; j < now.s.length(); j++){
bool f = true;
for(int k = ; k < rules[i].a.length(); k++){
if(j + k >= now.s.length()){
f = false;
break;
}
if(now.s[j + k] != rules[i].a[k]){
f = false;
break;
}
}
if(f)pos.push_back(j);
} //cout<<pos.size()<<endl;
for(int x = ; x < pos.size(); x++){
int p = pos[x];
char t[];
int j;
for(j = ; j < p; j++){
t[j] = now.s[j];
//cout<<t[j];
}
for(int k = ; k < rules[i].b.length(); k++, j++)
{
t[j] = rules[i].b[k];
}
for(int k = p + rules[i].a.length(); k < now.s.length(); k++, j++){
t[j] = now.s[k];
}
t[j] = '\0'; if(sset.find(t) == sset.end()){
que.push(node(t, now.steps + ));
sset.insert(t);
}
}
}
}
if(flag){
printf("%d\n", steps);
}
else{
printf("NO ANSWER!\n");
}
return ;
}

---恢复内容结束---

洛谷P1032 字串变换【bfs】的更多相关文章

  1. [洛谷P1032] 字串变换

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

  2. 洛谷 P1032 字串变换题解

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

  3. 洛谷 P1032 字串变换

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

  4. 洛谷 P1032 字串变换 (BFS)

    题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...

  5. 洛谷 P1032 字串变换 题解

    每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...

  6. 洛谷 P1032 字串变换(map)

    题目传送门 解题思路: 搜索题,因为要求最少次数,用bfs. AC代码: #include<cstdio> #include<iostream> #include<cst ...

  7. 集训作业 洛谷P1032 字串变换

    集训的题目有点多,先写困难的绿题吧(简单的应该想想就会了) 嗯,这个题看起来像个搜索呢(就是个搜索) 我们仔细想想就知道这个题肯定不能用深搜,可以优化的地方太少了,TLE是必然的. 那我们该怎么办呢? ...

  8. 洛谷P1032 字串变换-题解

    https://www.luogu.org/problemnew/show/P1032--(题目传送) 好在数据范围很小,暴力一点也能过.思路较简单,按照所有规则,从第一位开始广搜. 注意:1.str ...

  9. luogu题解P1032字串变换--BFS+STL:string骚操作

    题目链接 https://www.luogu.org/problemnew/show/P1032 分析 这题本来很裸的一个BFS,发现其中的字符串操作好烦啊.然后就翻大佬题解发现用STL中的strin ...

随机推荐

  1. O_DIRECT与O_SYNC区别(转)

    O_DIRECT和O_SYNC是系统调用open的flag参数.通过指定open的flag参数,以特定的文件描述符打开某一文件. 这两个flag会对写盘的性能有很大的影响,因此对这两个flag做一些详 ...

  2. LeetCode Permutations问题详解

    题目一 permutations 题目描述 Given a collection of numbers, return all possible permutations. For example,[ ...

  3. 【MySQL】玩转触发器、监听器

    1.触发器是一个特殊的存储过程,不同的是存储过程要用CALL来调用,而触发器不需要使用CALL. 创建触发器 语法如下: CREATE TRIGGER trigger_name trigger_tim ...

  4. openssl - X509证书操作函数

    原文链接: http://blog.csdn.net/zqt520/article/details/26965797 现有的证书大都采用X.509规范,主要同以下信息组成:版本号.证书序列号.有效期. ...

  5. ubuntu 16 安装 openjdk 8

    apt--jdk -y 进行验证即可

  6. App界面设计规范-字体规范

    通过对不同类型的app进行总结,总结出app的字体规范. 一.字体选择 1.IOS:苹果ios 9系统开始,系统最新的默认中文字体是:苹方.英文字体是: San Francisco 2.Android ...

  7. Spring Boot 2.0 整合Thymeleaf 模板引擎

    本节将和大家一起实战Spring Boot 2.0 和thymeleaf 模板引擎 1. 创建项目 2. 使用Spring Initlizr 快速创建Spring Boot 应用程序 3. 填写项目配 ...

  8. c++ clr编译dll在c#调用时出现“试图加载不正确的格式”“找不到dll”错误的解决

    用depends发现缺了一堆API-MS-WIN什么的dll,网上查找是因为少了VC++2010,VC++2015等一系列,装好后仍然不行,原来这种错误并不是该原因导致的,也并不缺少那些dll(dep ...

  9. java.lang.NoSuchFieldError: No static field abc_ic_ab_back_mtrl_am_alpha of type I in class Landroid/support/v7/appcompat/R$drawable

    出现java.lang.NoSuchFieldError: No static field abc_ic_ab_back_mtrl_am_alpha of type I in class Landro ...

  10. jQuery的ID选择器失效问题

    jQuery的ID选择器,在同一项目别的文件中一切正常: 在当前页面,jQuery的其它功能(如:$(document).ready(function(){  alert("ok" ...