当前位置:网站首页>字符串匹配问题
字符串匹配问题
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);
}
边栏推荐
- 万物生长大会在杭召开,当贝入选2022中国未来独角兽TOP100榜单
- SystemServer进程
- Certik released the defi security report in 2021, disclosing key data of industry development (PDF download link attached)
- Some interview suggestions for Android programmers "suggestions collection"
- Do you know that there is an upper limit on the size of Oracle data files?
- qt中uic的使用
- OpenHarmony笔记-----------(四)
- < 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
- NLA自然语言分析实现数据分析零门槛
- C crystal report printing
猜你喜欢
随机推荐
Everyone believes that the one-stop credit platform makes the credit scenario "useful"
Téléchargement par navigateur
Generally speaking, if the error of inconsistent tab and space occurs frequently
kaggle如何使用utility script
Data Lake (11): Iceberg table data organization and query
uni-app中使用computed解决了tab切换中data()值显示的异常
Quarkus学习四 - 项目开发到部署
无主灯设计:如何让智能照明更加「智能」?
[deep learning] simple implementation of neural network forward propagation
[template] longest common subsequence ([DP or greedy] board)
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
Qt如何设置固定大小
联合搜索:搜索中的所有需求
【文档树、设置】字体变小
In 2021, the global styrene butadiene styrene (SBS) revenue was about $3722.7 million, and it is expected to reach $5679.6 million in 2028
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
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
Qt-制作一个简单的计算器-实现四则运算
< 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
Analysis of CPU surge in production environment service







