当前位置:网站首页>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 :
边栏推荐
- Stress testing with locust on rainbow
- One of the touchdesigner uses - Download and install
- TRTC zero foundation -- Video subscription on the code
- Salesforce heroku (V) application in salesforce (canvasapp)
- Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]
- mysql 数据恢复 (.ibdata1, bin log)
- Regeorg actual attack and defense
- Jmeter- (V) simulated user concurrent login for interface test
- centos7 安装 MySQL 及配置 innodb_ruby
- JS how to delete an item specified in an array
猜你喜欢

第一批00后下场求职:不要误读他们的“不一样”

Gakataka student end to bundle Version (made by likewendy)

Analysis on the development of China's satellite navigation industry chain in 2021: satellite navigation is fully integrated into production and life, and the satellite navigation industry is also boo

Hierarchical attention graph convolution network for interpretable recommendation based on knowledge graph

JS Part 4

Analysis on the development of duty-free industry in Hainan Province in 2021: the implementation of the new policy makes the duty-free market in Hainan more "prosperous" [figure]
![Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]](/img/84/192d152ceb760264b6b555b321f129.jpg)
Analysis on the development prospect of China's brain computer interface industry in 2021: wide application prospect, sustained and rapid growth of market scale [figure]

LRU cache

centos7 安装 MySQL 及配置 innodb_ruby
![Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]](/img/de/d73805aaf4345ca3d2a7baf85aab8d.jpg)
Analysis of China's integrated circuit industry chain in 2021: huge downstream market demand [figure]
随机推荐
TRTC setaudioroute invalid problem
Uploading logs using loghub log4j appender
mysql常用指令
Tcapulusdb Jun · industry news collection (III)
Configuring multi cluster management using kubectl
Learning summary of spanquery source code
第一批00后下场求职:不要误读他们的“不一样”
【LeetCode】23. 合并K个升序链表
The compatibility of remotefx schemes is related to multiple factors
What are the advantages and difficulties of introducing AI into ISP Technology
Analysis on demand and market scale of China's steamed stuffed bun industry in 2020 [figure]
Tcapulusdb Jun · industry news collection (IV)
Chapter V kubernetes quick start
Customization of openfeign
What is the difference between the poll () method and the remove () method?
Troubleshooting and resolution of asydss virtual live broadcast status synchronization and service downtime
Simple analysis of easygbs compatible with old version HLS streaming address method [with code]
Brief introduction to arm architecture
Intelligent voice climbing patio
Easygbs service is killed because the user redis is infected with the mining virus process. How to solve and prevent it?