codevs1099 字串变换
题目描述 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$。
输入格式如下:
A$ B$
A1$ B1$ \
A2$ B2$ |-> 变换规则
... ... /
所有字符串长度的上限为 20。
若在 10 步(包含 10步)以内能将 A$ 变换为 B$ ,则输出最少的变换步数;否则输出"NO ANSWER!"
abcd xyz
abc xu
ud y
y yz
3
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 字串变换的更多相关文章
- codevs1099字串变换(Bfs)
/* 最少步数问题 妥妥的Bfs 很显然队列里存的是串(可能存个数也可以 就像8数码那样) 然后每次队首元素弄出来 能换的都换一遍 最后每次换完的新串入队前先判断到头了没 最后说一句 String大法 ...
- 「NOIP2002」「Codevs1099」 字串变换(BFS
1099 字串变换 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 已知有两个字串 $A$, ...
- NOIP2002字串变换[BFS]
题目描述 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ 可以变换为 B1$.A2 ...
- 字串变换(codevs 1099)
题目描述 Description 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ ...
- NOIP2002 字串变换
题二 字串变换 (存盘名: NOIPG2) [问题描述]: 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为: ...
- 字串变换 (2002 年NOIP全国联赛提高组)
一道看似非常水的题 大意 :将一个字串 经过几种变换规则变为给定的另一个子串 ,求最小操作数. code[vs] 传送门 洛谷传送门 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): ...
- NOIP 2002 提高组 字串变换
题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换为 B ...
- 【洛谷1032 】【CJOJ1711】【NOIP2002】字串变换
###题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B1.A2 可以变换 ...
- [NOIP2002]字串变换 T2 双向BFS
题目描述 已知有两个字串 A,B 及一组字串变换的规则(至多6个规则): A1−>B1 A2−>B2 规则的含义为:在 A$中的子串 A1可以变换为可以变换为B1.A2可以变换为可 ...
随机推荐
- spring cloud config搭建说明例子(四)-补充配置文件
服务端 ConfigServer pom.xml <dependency> <groupId>org.springframework.cloud</groupId> ...
- daily_journal_2 神奇的一天
写博客日记的第二天,第一天立的flag开始有点松动啦,继续坚持啊!坚持就是胜利. 今天真是神奇的一天,上午的计划是照常进行的,但是前天淋雨赶上风寒,又吃了新疆室友的大补特产,龙体开始感觉到不适,于是上 ...
- 题解报告:hdu 1015 Safecracker
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kl ...
- Tomcat6和7版本对web.xml中taglib标签的配置差异
原来部署在Tomcat6中的应用在Tomcat7中运行时报错如下错误: java.lang.IllegalArgumentException: taglib definition not consis ...
- Android 性能优化(7)网络优化( 3) Optimizing User-Initiated Network Use
Optimizing User-Initiated Network Use This lesson teaches you to Pre-fetch Network Data Check for Co ...
- Hbase源码分析:server端RPC
server端rpc包括master和RegionServer.接下来主要梳理一下,master和regionserver中有关rpc创建,启动以及处理的过程. 1,server rpc的初始化过程 ...
- 60查找nanopim1plus的HDMI为720p输出的问题
60查找nanopim1plus的HDMI为720p输出的问题 大文实验室/大文哥 壹捌陆捌零陆捌捌陆捌贰 21504965 AT qq.com 完成时间:2017/12/5 17:51 版本:V1. ...
- Ubuntu 16.04安装Kate文本编辑工具
Kate支持很多语言,比如NASM,比SBL3低那么一点,但是比Gedit好. 安装: sudo apt-get install kate 启动: 额外配置: 1.安装Kwrite sudo apt- ...
- Angular——MVC模式开发实战
创建项目 创建工作目录 使用bower下载需要插件 git init.add.commit之后得到分支master,再创建developer分支,然后再此分支上进行具体功能开发 MVC架构 之前小项目 ...
- Digital design之Boolean Algebra
1. 0 and 1 (duality: 0 -- 1, · -- +) X + 0 = X, X · 1 = X X + 1 = 1, X · 0 = 0 2. Idempotent X + X = ...