当前位置:网站首页>January 17, 2022: word rule II. Give you a pattern and a character
January 17, 2022: word rule II. Give you a pattern and a character
2022-06-23 03:35:00 【Fuda scaffold constructor's daily question】
2022-01-17: Rules of words II.
Give you a rule pattern And a string str, Please judge str Whether to follow the same rules .
Here we mean Follow exactly , for example pattern Each letter and string in str Each of them Non empty Between words , There is a corresponding law of two-way connection .
Power button 291.
answer 2022-01-17:
recursive .str=abcabc,pattern=xx. First, let a=x, let ab=x, let abc=x. Until the exact match .
The code to use golang To write . The code is as follows :
package main
import "fmt"
func main() {
ret := wordPatternMatch("abab", "redblueredblue")
fmt.Println(ret)
}
func wordPatternMatch(pattern, str string) bool {
return match(str, pattern, 0, 0, make([]string, 26), make(map[string]struct{}))
}
func match(s, p string, si, pi int, map0 []string, set map[string]struct{}) bool {
if pi == len(p) && si == len(s) {
return true
}
// str and pattern, It's not all over !
if pi == len(p) || si == len(s) {
return false
}
// str and pattern, It's not over !
//char ch = p.charAt(pi);
ch := p[pi]
cur := map0[ch-'a']
if cur != "" { // At present p[pi] It has been specified !
return si+len(cur) <= len(s) && // Don't cross the border !
cur == s[si:si+len(cur)] &&
match(s, p, si+len(cur), pi+1, map0, set)
}
// p[pi] It's not specified !
end := len(s)
// prune ! Important pruning !
for i := len(p) - 1; i > pi; i-- {
//end -= map0[p[i] - 'a'] == nil ? 1 : len(map0[p[i]-'a'])
if map0[p[i]-'a'] == "" {
end -= 1
} else {
end -= len(map0[p[i]-'a'])
}
}
for i := si; i < end; i++ {
// from si All prefix strings starting , Full test
cur = s[si : i+1]
// however , Only this prefix string , I haven't occupied any other pit before ! To try
if _, ok := set[cur]; !ok {
//set.add(cur);
set[cur] = struct{}{}
map0[ch-'a'] = cur
if match(s, p, i+1, pi+1, map0, set) {
return true
}
map0[ch-'a'] = ""
//set.remove(cur);
delete(set, cur)
}
}
return false
}The results are as follows :
边栏推荐
- Nezha panel modifies logo, small icon and other information
- Installing serverstatus probe using pagoda
- Chapter IV open source projects and deployment
- centos7 安装 MySQL 及配置 innodb_ruby
- If there is a smart bus visualization platform, can "beginning" restart indefinitely?
- 【机器学习】 吴恩达机器学习作业 ex2逻辑回归 Matlab实现
- What are the advantages and difficulties of introducing AI into ISP Technology
- [burning] Tencent cloud high tech computing platform HTPC cloud elastic cluster release!
- How to solve the problem that easynvr cannot be cascaded to the easynvs platform? View ports first
- Email authentication bypass
猜你喜欢
![Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]](/img/ca/672bfe49c8123da8679b2abeb43a2e.jpg)
Analysis on the development status of China's watch industry in 2021: a large number of electric watches are imported [figure]

【贪心】leetcode991. Broken Calculator

Jmeter- (V) simulated user concurrent login for interface test

Gakataka student end to bundle Version (made by likewendy)

innodb_ruby 视角下 MySQL 记录增删改

Static code block, code block, constructor execution order

mysql 数据恢复 (.ibdata1, bin log)

新版kali切换最高账户

Svn local computer storage configuration

One of the touchdesigner uses - Download and install
随机推荐
Why don't I suggest you use "! = null" to judge empty space?
How to print multiple barcode labels on one sheet of paper
页面导出excel的三种方式
Mybatties plus batch warehousing
How to implement collection sorting?
【LeetCode】23. 合并K个升序链表
Get method of fetch request and data of formdata type submitted by post
d重载嵌套函数
Analysis on development history, industrial chain, output and enterprise layout of medical polypropylene in China in 2020 [figure]
数据交易怎样实现
数据加密技术之源代码加密
Firewall and IP security policy configuration
Quickly understand the development status of secondary nodes of industrial Internet identity analysis
Copy system disk
Troubleshooting and resolution of asydss virtual live broadcast status synchronization and service downtime
Installing serverstatus probe using pagoda
This point (II)
1-1VMware介绍
Enterprise official website applet building tutorial
The difference between code39 and code93