当前位置:网站首页>字符串匹配问题
字符串匹配问题
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);
}
边栏推荐
- P1042 [noip2003 popularization group] Table Tennis
- Golang 快速生成数据库表的 model 和 queryset
- Yolov3 & yolov5 output result description
- freemarker的使用
- 如何设置Qt手工布局
- The most complete analysis of Flink frame window function
- How many knowledge points can a callable interface have?
- php链表创建和遍历
- Openharmony notes --------- (4)
- [deep learning] simple implementation of neural network forward propagation
猜你喜欢
【文档树、设置】字体变小

Chinese science and technology from the Winter Olympics (III): the awakening and evolution of digital people

Packet capturing tool Fiddler learning

kaggle如何使用utility script

Daily learning 2

< 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 new project

SystemServer进程
![[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)](/img/de/7d70f513577e93f1bde1969935a29e.jpg)
[development environment] 010 editor tool (tool download | binary file analysis template template installation | shortcut key viewing and setting)

php链表创建和遍历
随机推荐
每日学习2
Use of swagger
千元投影小明Q1 Pro和极米NEW Play谁更好?和哈趣K1比哪款配置更高?
Pycharm连接远程服务器
一般来讲,如果频繁出现inconsistent tab and space的报错
当贝投影4K激光投影X3 Pro获得一致好评:万元投影仪首选
无主灯设计:如何让智能照明更加「智能」?
瀏覽器驅動的下載
Solve the problem that openocd fails to burn STM32 and cannot connect through SWD
SystemServer进程
Story points vs. human days
MySQL45讲——学习极客时间MySQL实战45讲笔记—— 05 | 深入浅出索引(下)
2022 home projector preferred! Dangbei F5 brings the ultimate audio-visual experience with its powerful audio-visual effect
Getting started with QT - making a simple calculator
Adhere to the foundation of 20 minutes go every day II
Generally speaking, if the error of inconsistent tab and space occurs frequently
OpenHarmony笔记-----------(四)
Characteristics of selenium
TeamTalk源码分析之win-client
PyQt5_ Qscrollarea content is saved as a picture