当前位置:网站首页>字符串匹配问题
字符串匹配问题
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);
}
边栏推荐
- <口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
- 《可供方案开发》口算训练机/数学宝/儿童口算宝/智能数学宝 LCD液晶显示驱动IC-VK1622(LQFP64封装),原厂技术支持
- Qt新项目_MyNotepad++
- selenium 在pycharm中安装selenium
- Slashgear shares 2021 life changing technology products, which are somewhat unexpected
- Multi rotor aircraft control using PID and LQR controllers
- Thymeleaf dependency
- Data consistency between redis and database
- 快解析:轻松实现共享上网
- Launcher startup process
猜你喜欢
Packet capturing tool Fiddler learning
Code implementation MNLM
php链表创建和遍历
ensp简单入门
QT new project_ MyNotepad++
提示:SQL Server 阻止了对组件‘Ad Hoc Distributed Queries ‘的STATEMENT ‘OpenRowset/OpenDatasource“”
Getting started with QT - making a simple calculator
Penetrate the remote connection database through the Intranet
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
随机推荐
Qt原代码基本知识
Characteristics of selenium
Teamtalk source code analysis win client
<口算練習機 方案開發原理圖>口算練習機/口算寶/兒童數學寶/兒童計算器 LCD液晶顯示驅動IC-VK1621B,提供技術支持
Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people
uni-app中使用computed解决了tab切换中data()值显示的异常
Chaos engineering platform chaosblade box new heavy release
Qt入门-制作一个简易的计算器
docker mysql
OpenHarmony笔记-----------(四)
Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
PHP linked list creation and traversal
docker mysql
Data consistency between redis and database
Penetrate the remote connection database through the Intranet
默认插槽,具名插槽,作用域插槽
万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
跨服务器数据访问的创建链接服务器方法
Word frequency statistics & sorting
NLA自然语言分析,让数据分析更智能