当前位置:网站首页>字符串匹配问题
字符串匹配问题
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);
}
边栏推荐
- 软件测试的方法
- selenium的特点
- 2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
- Who is better, Qianyuan projection Xiaoming Q1 pro or Jimi new play? Which configuration is higher than haqu K1?
- Everyone believes that the one-stop credit platform makes the credit scenario "useful"
- Qt新建项目
- 万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
- Solving the longest subsequence with linear DP -- three questions
- QT - make a simple calculator - realize four operations
- Launcher startup process
猜你喜欢
瀏覽器驅動的下載
Development skills of rxjs observable custom operator
Launcher startup process
Available solution development oral arithmetic training machine / math treasure / children's oral arithmetic treasure / intelligent math treasure LCD LCD driver ic-vk1622 (lqfp64 package), original te
Error: eacces: permission denied, access to "/usr/lib/node_modules"
ensp简单入门
YOLOv3&YOLOv5输出结果说明
Qt新建项目
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
Getting started with QT - making a simple calculator
随机推荐
docker mysql
自定义事件,全局事件总线,消息订阅与发布,$nextTick
<口算练习机 方案开发原理图>口算练习机/口算宝/儿童数学宝/儿童计算器 LCD液晶显示驱动IC-VK1621B,提供技术支持
Origin plots thermogravimetric TG and differential thermogravimetric DTG curves
The global special paper revenue in 2021 was about $27 million, and it is expected to reach $35 million in 2028. From 2022 to 2028, the CAGR was 3.8%
线性dp求解 最长子序列 —— 小题三则
qt中uic的使用
给Android程序员的一些面试建议「建议收藏」
卷积神经网络(入门)
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
P1042 [NOIP2003 普及组] 乒乓球
浏览器驱动的下载
Methods of software testing
【虹科技术分享】如何测试 DNS 服务器:DNS 性能和响应时间测试
< schéma de développement de la machine d'exercice oral > machine d'exercice oral / trésor d'exercice oral / trésor de mathématiques pour enfants / lecteur LCD de calculatrice pour enfants IC - vk1621
QT - make a simple calculator - realize four operations
BeanUtils -- shallow copy -- example / principle
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
Some interview suggestions for Android programmers "suggestions collection"
测试框架TestNG的使用(二):testNG xml的使用