字符相等(E - 暴力求解、DFS)

本文介绍了一种新的字符串相等性的定义及判断方法。通过深度优先搜索算法来判断两个等长字符串是否符合特定条件下的相等性。文章提供了一个C++实现的例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断字符相等 

 

Description

Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases:

  1. They are equal.
  2. If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct:            
    1. a1 is equivalent to b1, and a2 is equivalent to b2
    2. a1 is equivalent to b2, and a2 is equivalent to b1

As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.

Gerald has already completed this home task. Now it's your turn!

Input

The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200 000 and consists of lowercase English letters. The strings have the same length.

Output

Print "YES" (without the quotes), if these two strings are equivalent, and "NO" (without the quotes) otherwise.

Sample Input

 
Input
aaba abaa
Output
YES
Input
aabb abab
Output
NO

Sample Output

 

Hint

In the first sample you should split the first string into strings "aa" and "ba", the second one — into strings "ab" and "aa". "aa" is equivalent to "aa"; "ab" is equivalent to "ba" as "ab" = "a" + "b", "ba" = "b" + "a".

In the second sample the first string can be splitted into strings "aa" and "bb", that are equivalent only to themselves. That's why string "aabb" is equivalent only to itself and to string "bbaa".

 

题意:

两个字符串a,b等长称为相等。

1.a,b是直接相等的

2.a,b可以分为相同大小的两半,a1.a2.b1.b2,下面有一个是正确的:

            1.a1=b1  且 a2=b2

            2.a1=b2且a2=b1

判断两个字符串是否相等。

 

分析:

1.DFS(深度优先搜索)

2.字符串长度为奇数时直接比较两个字符串相不相等;字符串长度为偶数时,平分字符串,比较,如果不相等继续分,一直到字符串长度为奇数。

 

代码:

 1  1 #include<cstdio>
 2  2 #include<iostream>
 3  3 #include<cstring>
 4  4 using namespace std;
 5  5 const int maxn=200001;
 6  6 
 7  7 char a[maxn],b[maxn];
 8  8 
 9  9 bool dfs(char *p1,char *p2,int len)
10 10 {
11 11     if(!strncmp(p1,p2,len))
12 12         return true;
13 13     if(len%2)       //判断字符串长度是奇是偶
14 14         return false;
15 15     int n=len/2;    //将字符串平均分为两部分
16 16     if(dfs(p1,p2+n,n)&&dfs(p1+n,p2,n)) 
17 17         return true;
18 18     if(dfs(p1,p2,n)&&dfs(p1+n,p2+n,n)) //字符串比较
19 19         return true;
20 20     return false;
21 21 
22 22 }
23 23 
24 24 int main()
25 25 {
26 26     scanf("%s%s",a,b);
27 27     printf("%s\n",dfs(a,b,strlen(a))?"YES":"NO");
28 28     return 0;
29 29 }
30 View Code
View Code


又是看了别人的代码。好想自己写出来。

转载于:https://www.cnblogs.com/ttmj865/p/4690525.html

### 关于DFS和BFS的经典练习题目及应用场景 #### 经典练习题目 1. **岛屿数量 (LeetCode 200)** 题目描述:给定一个由 '1'(陆地)和 '0'(水)组成的二维网格,计算岛屿的数量。岛屿被水包围,并通过水平或垂直连接形成。可以使用 DFS 或 BFS 来解决此问题[^1]。 2. **括号的有效组合 (LeetCode 301)** 题目描述:移除非法的括号使得字符串合法化,求所有可能的结果集合。由于目标是最少删除次数下的解集,因此更适合采用 BFS 方法来按层次遍历并找到最短路径解决方案[^2]。 3. **克隆图 (LeetCode 133)** 使用 DFS 或 BFS 对无向连通图进行深拷贝操作。可以通过递归调用 `dfs` 函数完成节点复制过程;或者利用队列存储待访问结点的方式执行广搜版本的任务处理逻辑[^3]。 4. **洪水填充算法 (LeetCode 733)** 提供了一幅RGB颜色表示的整数矩阵作为输入参数之一以及指定起点位置坐标(sr,sc),还有新颜料编号newColor,则需按照规则修改原图画布上的对应区域像素值至新的色彩标记。这里展示了如何基于特定条件扩展相邻单元格的应用实例——即当两个方格共享边且它们原有的数值相等时才继续深入探索其邻域范围内的其他候选对象直至满足停止准则为止[^4]。 #### 应用场景分析 - **迷宫求解/路径规划** - 当面对寻找两点间是否存在可达关系的问题情境下,无论是判断简单联通性还是寻求最优路线方案,都可以借助这两种基本图形遍历技术加以实现。 - **社交网络中的朋友推荐系统构建** - 假设我们拥有某个用户的直接好友列表数据结构形式表达出来的话,在此基础上进一步挖掘间接关联成员群体就成为了必要环节。此时运用深度优先策略能够快速定位潜在候选人选群组;而如果希望获取距离当前主体较近的一圈紧密联系伙伴则更加倾向于选择宽度优先方法论来进行高效检索工作。 - **编译器依赖解析管理工具开发** - 在软件工程项目里经常遇到模块之间存在复杂的相互依存状况需要妥善安排加载顺序等问题场合之下,合理选用合适的搜索模式有助于提高整体效率表现同时简化代码维护难度等方面均具有重要意义价值体现之处所在。 ```python def bfs(graph, start): queue = [start] visited = set([start]) result = [] while queue: node = queue.pop(0) result.append(node) for neighbor in graph[node]: if neighbor not in visited: visited.add(neighbor) queue.append(neighbor) return result def dfs_recursive(graph, start, visited=None): if visited is None: visited = set() visited.add(start) for next_node in graph[start]: if next_node not in visited: dfs_recursive(graph, next_node, visited) return list(visited) # Example usage of both algorithms on a sample graph represented as adjacency lists. sample_graph = { 'A': ['B', 'C'], 'B': ['D', 'E'], 'C': ['F'], 'D': [], 'E': ['F'], 'F': [] } print("Breadth First Search:", bfs(sample_graph, 'A')) print("Depth First Search Recursive:", dfs_recursive(sample_graph, 'A')) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值