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

思路

初看题目觉得挺简单的一道题

但是仔细想了一下发现实现代码挺麻烦的

而且2002年的毒瘤输入是什么鬼啊 连组数都没有的真的好吗

这道题参考了题解才完成

需要用到我从来没有用过的map来判重

然后就是广搜+string的一些自带函数运用

PS:这道题本地测试数据时要用Ctrl+Z+回车才可以出ans

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
using namespace std;
#define maxn 15
map<string,int> m;//map用来判重
string a,b;
string c[maxn],d[maxn];
int n,ans;
struct node//自定义结构体方便调用
{
string str;//字符串
int sum;//步数
};
queue <node> q;//队列
string change(const string &str,int i,int j)
{
string t="";
if(i+c[j].length()>str.length()) return t;//长度超过就不可以替换
for(int k=;k<c[j].length();k++)//枚举判断是否可以替换
if(str[i+k]!=c[j][k]) return t;
t=str.substr(,i);//新串的前面没有改变
t+=d[j];//加上替换之后的串
t+=str.substr(i+c[j].length());//加上需要替换的串的后面不需要替换的串
return t;
}
int main()
{
cin>>a>>b;
while(cin>>c[n]>>d[n]) n++;//毒瘤输入
node s;
s.str=a;//初始的串
s.sum=;
q.push(s);
while(!q.empty())//BFS
{
node x=q.front();//取出队列头
q.pop();//删除头
string temp;
if(m.count(x.str)==) continue;//判重
if(x.str==b) //如果已经满足
{
ans=x.sum;
break;
}
m[x.str]=;//这种字符串已经尝试过
for(int i=;i<x.str.length();i++)//从头枚举哪里可以替换
{
for(int j=;j<n;j++)//枚举替换方案
{
temp=change(x.str,i,j);//替换
if(temp!="")//如果可以替换
{
node y;//加入队列
y.str=temp;
y.sum=x.sum+;
q.push(y);
}
}
}
}
if(ans>||(!ans))//注意ans同样不能为0
cout<<"NO ANSWER!";
else
cout<<ans;
}

【题解】洛谷P1032 [NOIP2002TG]字串变换(BFS+字符串)的更多相关文章

  1. 洛谷 P1032 【字串变换】

    感觉这个题用一些常用的stl和string函数会非常简单..(难道就是考这两个的吗? vector<pair<string,string>>pos//用于变化 map<s ...

  2. 字串变换 bfs + 字符串

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

  3. 【洛谷P3411】字串变换

    题解:普通的 BFS 没什么可说的,字符串处理是这道题的难点,同时需要注意哈希判重. 另外,对于 \(string\) 类来说,学到了一个 push_back((char)) 操作. c++strin ...

  4. NOIP2002字串变换[BFS]

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

  5. 洛谷P2408 不同字串个数 [后缀数组]

    题目传送门 不同字串个数 题目背景 因为NOI被虐傻了,蒟蒻的YJQ准备来学习一下字符串,于是它碰到了这样一道题: 题目描述 给你一个长为N的字符串,求不同的子串的个数 我们定义两个子串不同,当且仅当 ...

  6. 洛谷2408不同字串个数/SPOJ 694/705 (后缀数组SA)

    真是一个三倍经验好题啊. 我们来观察这个题目,首先如果直接整体计算,怕是不太好计算. 首先,我们可以将每个子串都看成一个后缀的的前缀.那我们就可以考虑一个一个后缀来计算了. 为了方便起见,我们选择按照 ...

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

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

  8. 洛谷P4493 [HAOI2018]字串覆盖(后缀自动机+线段树+倍增)

    题面 传送门 题解 字符串就硬是要和数据结构结合在一起么--\(loj\)上\(rk1\)好像码了\(10k\)的样子-- 我们设\(L=r-l+1\) 首先可以发现对于\(T\)串一定是从左到右,能 ...

  9. [洛谷P1032] 字串变换

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

随机推荐

  1. 08_zookeeper的客户端工具curator_watcher

    [使用usingWatcher()] //两个usingWatcher的传入参数不同 public interface Watchable<T> { T watched(); T usin ...

  2. jso cpp解析读写数据

    json write : { "array" : [ { "aaaaaa" : "bbbbbb", "number" : ...

  3. Echarts实现隐藏x轴,y轴,刻度线,网格

    "yAxis": [ { //就是一月份这个显示为一个线段,而不是数轴那种一个点点 "show" : true, "boundaryGap" ...

  4. python mysql安装

    本文主要介绍不同系统mysql安装 mac安装mysql http://blog.csdn.net/pansanday/article/details/54915916   linux安装mysql ...

  5. 测试mysql性能工具

    mysqlslap mysqlslap可以模拟服务器的负载,并输出计时信息.它包含在MySQL 5.1 的发行包中,应该在MySQL 4.1或者更新的版本中都可以使用.测试时可以执行并发连接数,并指定 ...

  6. 【Leetcode】【Medium】Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given 1->2-&g ...

  7. 【Leetcode】【Medium】Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. Net编译原理简单

    转载:http://blog.csdn.net/sundacheng1989/article/details/20941893 首先简单说一下计算机软件运行.所谓软件运行,就是一步一步做一些事情.计算 ...

  9. 关于mysql启动问题---mysqld_safe mysqld from pid file * ended

    #在[mysqld]中添加: datadir = /usr/local/mysql/data         #添加 log-error = /usr/local/mysql/data/error.l ...

  10. 50. Pow(x, n) (recursion)

    Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...