【搜索】P1032 字串变换
题目描述
已知有两个字串A,B及一组字串变换的规则(至多6个规则):
A1 ->B1
A2 -> B2
规则的含义为:在 A中的子串 A1 可以变换为B1,A2 可以变换为 B2 …。
例如:A='abcd'B='xyz'
变换规则为:
‘abc’->‘xu’‘udud’->‘y’‘y’->‘yz’
则此时,A可以经过一系列的变换变为B,其变换的过程为:
‘abcd’->‘xud’->‘xy’->‘xyz’
共进行了3次变换,使得A变换为B。
输入输出格式
输入格式:
输入格式如下:
A B
A1 B1
A2 B2 |-> 变换规则
... ... /
所有字符串长度的上限为20。
输出格式:
输出至屏幕。格式如下:
若在10步(包含10步)以内能将A变换为B,则输出最少的变换步数;否则输出"NO ANSWER!"
输入输出样例
abcd xyz
abc xu
ud y
y yz
3
#include<bits/stdc++.h>
using namespace std;
const int N = ;
string A,B,a[N],b[N];
map<string,int>Mp; typedef struct Node{
string S;
int step;
}Node; int cnt = ;
int Check(string u,string v,int pos = ){
int L1 = u.length() ;
int L2 = v.length() ;
int i = pos , j = ;
while( i < L1 && j < L2 ){
while( u[i] == v[j] ){
i++;
j++;
if( j == L2 ){
return i-L2;
}
}
j = ;
i ++ ;
}
return -;
}
int ans = - ;
void bfs( ){ queue<Node>Q;
Q.push ( Node{A,} );
Mp[A] = ;
while ( !Q.empty() ){
Node cur = Q.front();
Q.pop();
if( cur.step >= ) continue;
if( cur.S == B ){
ans = cur.step;
return ;
}
for(int i=;i<cnt;i++){
string tmp = cur.S;
int Len = tmp.length();
int L = a[i].length();
for( int j= ; j < Len ; j++ ) {
int pos = Check(tmp,a[i],j);
if( pos != - && pos != ){
string s1 = tmp.substr(,pos);
string s2 = b[i];
string s3 = tmp.substr(pos+L);
string t = s1+s2+s3;
//cout<<t<<endl;
if( Mp[t] == ){
Mp[t] = ;
Q.push(Node{t,cur.step+});
}
}
if( pos == ){
string s1 = b[i];
string s2 = tmp.substr(pos+L);
string t = s1 + s2 ;
//cout<<t<<endl;
if( Mp[t] == ){
Mp[t] = ;
Q.push(Node{t,cur.step+});
}
}
}
}
}
}
int main() { ios_base :: sync_with_stdio(NULL);
cin.tie(NULL);
cout.tie(NULL); cin>>A>>B; while( cin>>a[cnt]>>b[cnt] ){ if( a[cnt] == "" && b[cnt]=="")
break;
cnt++;
}
bfs();
if(ans==-){
puts("NO ANSWER!");
}else{
printf("%d\n",ans);
}
return ;
}
【搜索】P1032 字串变换的更多相关文章
- P1032 字串变换 字符串BFS
题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A_2A2 -> B_2B2 规则的含义为:在 AA中的子串 A_1A1 ...
- 洛谷 P1032 字串变换题解
题目链接:https://www.luogu.org/problem/P1032 题目描述 已知有两个字串A,BA,B及一组字串变换的规则(至多66个规则): A_1A1 ->B_1B1 A ...
- 洛谷 P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- [洛谷P1032] 字串变换
洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...
- luogu P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- [NOIP2002] 提高组P1032 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- 洛谷 P1032 字串变换 (BFS)
题目传送门 我即使是死了,钉在棺材里了,也要在墓里,用这腐朽的声带喊出 STL大法好 这题最麻烦的其实是处理字符串,真正的搜索部分我个人认为也就只有橙题或黄题的难度.而处理字符串,正如前面所说,STL ...
- 洛谷 P1032 字串变换 题解
每日一题 day19 打卡 Analysis 广搜+map判重 用find寻找字串,再用replace替换字串 这里的map相当于正常广搜的一个book的作用 #include<iostream ...
- 【洛谷】P1032 字串变换
题目地址:https://www.luogu.org/problemnew/show/P1032 洛谷训练场BFS的训练题呀. “BFS不就是用队列的思想去遍历一切情况嘛.我已经不是小孩子了,我肯定能 ...
随机推荐
- sql常识性误解
今天在公司一个项目,遇到一个问题,最后解决下来竟然发现自己对sql竟然存在一个常识性的误解 表数据 需求如下 查找 name中的数据被参数 'adsb' 包含的的列 个人原先的误区一直在于一个认识, ...
- 卷积神经网络中的Winograd快速卷积算法
目录 写在前面 问题定义 一个例子 F(2, 3) 1D winograd 1D to 2D,F(2, 3) to F(2x2, 3x3) 卷积神经网络中的Winograd 总结 参考 博客:blog ...
- html行内要素与块级要素
行内要素:在一行里,不可设置width和height,不能上下外铺(margin) span 块状要素,标准的 div
- HDU4467:Graph(点的度数分块)
传送门 题意 给出一张n个点m条边的无向图,点的颜色为0/1,每次有两种操作: 1.Asksum x y,查询两点颜色为x和y的边的权值之和 2.Change x,将x颜色取反 分析 最直接的做法是每 ...
- 纯javaScript实现元素平滑滚动,改进前两个版本,支持鼠标滚轮滚动和点击元素滚动,滚动更顺畅
windowScroll(id, number, distance, direction, obj) 参数介绍: 1.id:所要滚动的元素id; 2.number:滚动次数; 3.distance:每 ...
- Luogu P1265修复公路【Prim最小生成树】By cellur925
题目传送门 政府审批的规则如下: (1)如果两个或以上城市申请修建同一条公路,则让它们共同修建: (2)如果三个或以上的城市申请修建的公路成环.如下图,A申请修建公路AB,B申请修建公路BC,C申请修 ...
- 交表(Send a Table)
#include<stdio.h> #include<string.h> #define N 50010 int phi[N],n,sum[N]; void phi_table ...
- Jquery | 基础 | 慕课网 | 元素选择器
getElementsByTagName方法得到页面所有的<div>元素 var divs = document.getElementsByTagName('div'); 与 同样的效果, ...
- js封装xhr【重复造轮子】
仿jquery ajax,不过功能没那么多.贴代码 --------------------------------------分割线--------------------------------- ...
- UvaLive6439(string使用、回文串)
样例手写一写很容易发现规律(前后一样的串,则ans+=2),实现起来却忘了string的便捷性,其实根本用不到哈希. ; int n, ans; string s, t1, t2; int main( ...