1099 字串变换

题目描述 Description

已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则):
     A1$ -> B1$
     A2$ -> B2$
  规则的含义为:在 A$中的子串 A1$ 可以变换为 B1$、A2$ 可以变换为 B2$ …。
    例如:A$='abcd' B$='xyz'
  变换规则为:
    ‘abc’->‘xu’ ‘ud’->‘y’ ‘y’->‘yz’

  则此时,A$ 可以经过一系列的变换变为 B$,其变换的过程为:
   ‘abcd’->‘xud’->‘xy’->‘xyz’

  共进行了三次变换,使得 A$ 变换为B$。

输入描述 Input Description

输入格式如下:

   A$ B$
   A1$ B1$ \
   A2$ B2$  |-> 变换规则
   ... ... / 
  所有字符串长度的上限为 20。

输出描述 Output Description

若在 10 步(包含 10步)以内能将 A$ 变换为 B$ ,则输出最少的变换步数;否则输出"NO ANSWER!"

样例输入 Sample Input

abcd xyz
abc xu
ud y
y yz

样例输出 Sample Output

3

数据范围及提示 Data Size & Hint

hehe 


//replace用法

/*用法一: 
 *用str替换指定字符串从起始位置pos开始长度为len的字符 
 *string& replace (size_t pos, size_t len, const string& str); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    line = line.replace(line.find("@"), 1, ""); //从第一个@位置替换第一个@为空  
    cout << line << endl;     
    return 0;  
}  
运行结果:


/*用法二: 
 *用str替换 迭代器起始位置 和 结束位置 的字符 
 *string& replace (const_iterator i1, const_iterator i2, const string& str); 
 */  
int main()  
{  
    string line = "this@ is@ a test string!";  
    line = line.replace(line.begin(), line.begin()+6, "");  //用str替换从begin位置开始的6个字符  
    cout << line << endl;     
    return 0;  
}  



compare使用
#include <iostream>    
#include <string>    
#include <cctype>    
using std::cout;    
using std::endl;    
using std::cin;    
using std::string;    
int main(void){    
    string str1="hi,test,hello";    
    string str2="hi,test";    
    //字符串比较    
    if(str1.compare(str2)>0)    
        printf("str1>str2\n");    
    else if(str1.compare(str2)<0)    
        printf("str1<str2\n");    
    else    
        printf("str1==str2\n");    
        
    //str1的子串(从索引3开始,包含4个字符)与str2进行比较    
    if(str1.compare(3,4,str2)==0)    
        printf("str1的指定子串等于str2\n");    
    else    
        printf("str1的指定子串不等于str2\n");    
        
    //str1指定子串与str2的指定子串进行比较    
    if(str1.compare(3,4,str2,3,4)==0)    
        printf("str1的指定子串等于str2的指定子串\n");    
    else    
        printf("str1的指定子串不等于str2的指定子串\n");    
        
    //str1指定子串与字符串的前n个字符进行比较    
    if(str1.compare(0,2,"hi,hello",2)==0)    
        printf("str1的指定子串等于指定字符串的前2个字符组成的子串\n");    
    else    
        printf("str1的指定子串不等于指定字符串的前2个字符组成的子串\n");    
    return 0;    
        
} 







#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
string st,a[8],b[8],en;
int t=1;
typedef struct node{
	string s;
	int step;
}NO;
queue<NO>q;
void bfs(){
	q.push((NO){st,0});
	while(!q.empty()&&q.front().step<10){
		NO f = q.front();
		q.pop();
		for(int i=1;i<t;i++){
			for(int k=0;k<f.s.length();k++){
				if(f.s.compare(k,a[i].size(),a[i])==0)//匹配字符串相等 
				{
					string tmp = f.s;
					tmp.replace(k,a[i].size(),b[i]);//c++指定位置替换 
					if(tmp==en){
						cout<<f.step+1;
						return ;
					}
					q.push((NO){tmp,f.step+1});
				}
				
			}
		}
	
	}
cout<<"NO ANSWER!";
	
}
int main(){
	cin>>st>>en;
	while(cin>>a[t]>>b[t])
	t++;
	bfs();
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值