题目描述 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

思路:
双向广搜
代码:
#include<stdio.h>
#include<string.h>
struct node
{
char s[];
int dep; //变换次数
} list1[], list2[];
char a[][], b[][];
int n;
void BFS()
{
int head1, tail1, head2, tail2, k;
head1 = tail1 = head2 = tail2 = ;
while(head1 <= tail1 && head2 <= tail2)
{
if(list1[head1].dep + list2[head2].dep > )
{
printf("NO ANSWER!\n");
return ;
}
for(int i = ;i < strlen(list1[head1].s); i++)
for(int j = ; j <= n; j++)
if(strncmp(list1[head1].s + i, a[j], strlen(a[j])) == ) //寻找当前可变换的规则
{
tail1++; //移动尾指针,存储变换后的字符串,以下三个for循环为变换过程
for(k = ; k < i; k++)
list1[tail1].s[k] = list1[head1].s[k];
for(int l = ; l < strlen(b[j]); l++, k++)
list1[tail1].s[k] = b[j][l];
for(int l = i + strlen(a[j]); l <= strlen(list1[head1].s); l++, k++)
list1[tail1].s[k] = list1[head1].s[l];
list1[tail1].s[k] = '\0'; //为变换结束后的字符串加结束符
list1[tail1].dep = list1[head1].dep+;
for (k = ; k <= tail1; k++)
if (strcmp(list1[tail1].s, list2[k].s) == )//判断当前状态是否与逆向搜索交汇
{
printf("%d\n", list1[tail1].dep + list2[k].dep);
return ;
}
}
for (int i = ; i < strlen(list2[head2].s); i++) //逆向搜索同上
for (int j = ; j <= n; j++)
if(strncmp(list2[head2].s + i, b[j], strlen(b[j])) == )
{
tail2++;
for(k = ; k < i; k++)
list2[tail2].s[k] = list2[head2].s[k];
for(int l = ; l < strlen(a[j]); l++, k++)
list2[tail2].s[k] = a[j][l];
for(int l = i + strlen(b[j]); l <= strlen(list2[head2].s); l++, k++)
list2[tail2].s[k] = list2[head2].s[l];
list2[tail2].s[k] = '\0';
list2[tail2].dep = list2[head2].dep + ;
for (k = ;k <= tail1; k++)
if (strcmp(list1[k].s, list2[tail2].s) == )
{
printf("%d\n",list1[k].dep + list2[tail2].dep);
return ;
}
}
head1++;
head2++;
}
printf("NO ANSWER!\n");
}
int main()
{
scanf("%s%s",list1[].s, list2[].s);
n = ;
while (scanf("%s%s",a[n],b[n]) != EOF)
n++;
n--;
list1[].dep = list2[].dep = ;
BFS();
return ;
}

codevs1099 字串变换的更多相关文章

  1. codevs1099字串变换(Bfs)

    /* 最少步数问题 妥妥的Bfs 很显然队列里存的是串(可能存个数也可以 就像8数码那样) 然后每次队首元素弄出来 能换的都换一遍 最后每次换完的新串入队前先判断到头了没 最后说一句 String大法 ...

  2. 「NOIP2002」「Codevs1099」 字串变换(BFS

    1099 字串变换 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 已知有两个字串 $A$, ...

  3. NOIP2002字串变换[BFS]

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

  4. 字串变换(codevs 1099)

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

  5. NOIP2002 字串变换

    题二 字串变换 (存盘名: NOIPG2) [问题描述]: 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为: ...

  6. 字串变换 (2002 年NOIP全国联赛提高组)

    一道看似非常水的题 大意 :将一个字串 经过几种变换规则变为给定的另一个子串 ,求最小操作数. code[vs] 传送门 洛谷传送门 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): ...

  7. NOIP 2002 提高组 字串变换

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

  8. 【洛谷1032 】【CJOJ1711】【NOIP2002】字串变换

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

  9. [NOIP2002]字串变换 T2 双向BFS

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

随机推荐

  1. 二分查找 HDOJ 2141 Can you find it?

    题目传送门 /* 题意:给出一个数,问是否有ai + bj + ck == x 二分查找:首先计算sum[l] = a[i] + b[j],对于q,枚举ck,查找是否有sum + ck == x */ ...

  2. Spring.Net学习笔记(二)-数据访问器

    Spring对ADO.NET也提供了支持,依赖与程序集Spring.Data.dll IDbProvider IDbProvider定义了数据访问提供器的基础,配置如下 <?xml versio ...

  3. 微信小程序一些常见的坑

    1.小程序都报wxss编译错误 解决方法: 在控制台输入openVendor() ,清除里面的wcsc wcsc.exe 然后重启工具 2.微信小程序wx:for警告 Now you can prov ...

  4. vue 父子组件双向绑定

    vue组件有2大特性: 1.全局组件和局部组件 2.父子组件的数据传递 接下来直接用demo直接看如何传值(静态传值) father.vue <template> <div> ...

  5. Android中 string.xml资源 如何添加参数?

    在android 开发,我们通常会用string.xml资源去设置textview等控件的字符串.而值一般是与程序的运行结果无关的. 但有时需要根据运行的结果来显示到控件中,这时字符串资源就不能写死了 ...

  6. ThinkPHP系统流程

    1.用户通过入口文件访问控制器2.控制器从模型层中提取数据3.控制器将数据返回模板页面

  7. JS高级——监听浏览器的返回事件

    https://www.cnblogs.com/Easty/p/7820055.html https://www.cnblogs.com/zhengyan/p/6912526.html http:// ...

  8. 关于vue构建项目的一些指令

    第一步: brew install nodejs(MAC机子下)  Windows直接官网下载对应版本node.js 第二步: 获取nodejs模块安装目录访问权限(Windows系统忽略)sudo ...

  9. Spartan6系列之Spartan6系列之芯片时钟资源深入详解

    1.   时钟资源概述 时钟设施提供了一系列的低电容.低抖动的互联线,这些互联线非常适合于传输高频信号.最大量减小时钟抖动.这些连线资源可以和DCM.PLL等实现连接. 每一种Spartan-6芯片提 ...

  10. java生成excel

    package test.poi; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; ...