当前位置:网站首页>字符串匹配问题
字符串匹配问题
2022-07-02 11:04:00 【N. LAWLIET】
问题:
给出一个输入字符串s(不含.和*)和一个表达式p(可以包含.和*).表示任何一个单个字符,不能表示空,*表示前面任何一个或多个子符,但不能单独使用。判断s 和p能否相等?
示例:
s=“aa”,p=“a*”
return true;
思想:
分情况讨论,当i+1位置不是“”时,返回true要满足j<s.length(),s[j]p[i]||p[i]‘.’,以及他们下一位置也满足条件,
第二种情况是i+1位置等于“”.这种p每次比较跳两个位置。
代码:
public static boolean Match(String str,String exp) {
if(str.length()==0||exp.length()==0) {
return false;
}
char[] s = str.toCharArray();
char[] e = exp.toCharArray();
return isVaild(s,e)&&process(s,e,0,0);
}
public static boolean isVaild(char[] s,char[] e) {
for(int i = 0;i<s.length;i++) {
if(s[i]=='.'||s[i]=='*') {
return false;
}
}
for(int i = 0;i<e.length;i++) {
if(e[i]=='*'&&(i==0||e[i-1]=='*')) {
return false;
}
}
return true;
}
public static boolean process(char[] s,char[] e,int sl,int el ) {
if(sl == s.length) {
return el == e.length;
}
//e[i]有字符,e[i+1]!="*"
if(el+1!=e.length&&e[el+1]!='*') {
return sl!=s.length&&(s[sl]==e[el]||e[el]=='.')&&process(s, e, sl+1, el+1);
}
//e[i]有字符,e[i+1]=="*"
while(sl<s.length&&(s[sl]==e[el]||e[el]=='.')) {
if(process(s, e, sl, el+2)) {
return true;
}
sl++;
}
return process(s, e, sl, el+2);
}
边栏推荐
- Data Lake (11): Iceberg table data organization and query
- Qt入门-制作一个简易的计算器
- Design of non main lamp: how to make intelligent lighting more "intelligent"?
- SystemServer进程
- Generally speaking, if the error of inconsistent tab and space occurs frequently
- 693. Travel sequencing (map + topology)
- MySQL 45 lecture - learning the actual battle of MySQL in Geek time 45 Lecture Notes - 05 | easy to understand index (Part 2)
- Stone merging Board [interval DP] (ordinary stone Merging & Ring Stone merging)
- c# 水晶报表打印
- P1042 [NOIP2003 普及组] 乒乓球
猜你喜欢
随机推荐
Solving the longest subsequence with linear DP -- three questions
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
Development skills of rxjs observable custom operator
瀏覽器驅動的下載
selenium 元素定位方法
Selenium element positioning method
docker mysql
Characteristics of selenium
The evolution process of the correct implementation principle of redis distributed lock and the summary of redison's actual combat
如何设置Qt手工布局
Use of UIC in QT
Route (II)
联合搜索:搜索中的所有需求
Analysis of CPU surge in production environment service
Launcher启动过程
php链表创建和遍历
抓包工具fiddler学习
Packet capturing tool Fiddler learning
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)
The conference on the growth of all things was held in Hangzhou, and dangbei was selected into the top 100 list of future unicorns in China in 2022
![[deep learning] simple implementation of neural network forward propagation](/img/a6/9b4896c62e9b77f9d528c3c9efc58f.jpg)







