coding-coworking-club / python-spring-2021

ccClub Python Spring 2021
https://www.ccclub.io/course/2021Spring
23 stars 6 forks source link

[General] 子字串辨識 #526

Closed CKJK47 closed 3 years ago

CKJK47 commented 3 years ago

提交連結

https://judge.ccclub.io/status/52e88872d7ec7005ac0336bde314a99b

程式碼

def isSubStr(s, t):
    import re
    new_s = ""
    #利用正規表達式的規則,把s字串每一個中間都插入0~多個字
    for i in s:
        new_s += i + "\w*"
    #使用re.match去比對兩個字串,若有回傳成功代表True,回傳None代表False
    if re.match(new_s, t) != None:
        return True
    else:
        return False

錯誤訊息

截圖 2021-06-07 下午3 16 51

問題描述

  1. 我是用有點偷吃步的方法,題目敘述要刪去t字串的字,我反向思考把s字串每個字中間都插入0~多個字母
  2. 利用正規表達式match這兩個字串,只要match就確保會符合s字串的順序
  3. judge跑出來是WA,不知道是極端值沒考慮還是這個方法邏輯有缺陷?謝謝助教
TJ72 commented 3 years ago

re.match(pattern, string) 好像在 pattern 的字串長度上有一點限制,如果字串長度太長,即使有 match 也會自動回傳 None

你可以試試底下的測資,回傳應該要是 True

abcdefghijklmnopqrstuvwxyz
evhwnolzeanniyhivfniyznwsguytmtuehsuibletttyyrulsxyinizfepbbaybdphvqtuabsxbhecormqofhehckoctgbtphhjvdrvjqnunmddytxwkbxurrxywqtndthbxyiodqwspljbrkwplkzwqcnuvydkluehxqhgotrqlggifwftpybyhagnlzxhwyleouvyngtdgisatbxnglmgnajvytbqxyulusinyogpfjbzohaxshfqlgqqaztznjpnowkaqmpzwhveefcshpcatwjyvazqvgscqjqiguwdzsybnaxfdprzjcqngcbmvrakhdcehzdpvsngjsatptdxdvylxiznjgkkcknvjnrhnvxwxqbbvettnqagltwhqvjoxqpjfpjsiivggugnkigflgxidgacmuhzhpysifaeggfvnwxhajaujrejekvunvnwlfyuynwqqizeoisimjihfonfinlwkvfrgnaheafwunjgkjvbreavsncghcjsqwarcnzoavlbaumyszhxlqdixaukgvvnufeonoicgmwutxltassuphgchhuddutiomckgisycheoonsbbcekngcmvuxzpiqcguhbqoaodfmniovaqmolbetsgvsyeahaqvjdbbstlpcsjhngiztwsilgixnssxmtxmjzzkywovuzcxyzmccgsssgziwsjdxgxgprksscoyoghtpbesjbdhpmptktsowxgoeettfsuavqvbihxltktzjxbaigeyrxjsytdzqdwtyoaurdglbmmkrksfdthrbhjbijfyqrpezpupzezllrxxjjtvaifnumjtipqpfurhlsbvhgocblahxaefhmvqnamzdbibypkmzseiwcmbtmzyohmnijjuhimohomzzixmdahbpmdmxgiojozoyksqnvocgsbxemygbwwklpreayeacdnifuuyrbogohhkmhubgsvizotnukxyjqnwjcvkmibmtllnxvz