美文网首页
201803-3(Python实现)

201803-3(Python实现)

作者: tmax | 来源:发表于2019-12-09 22:18 被阅读0次
    • 50分
    import re
    n, m = list(map(int, input().split()))
    rules = []
    tags = []
    urls = []
    for i in range(n):
        tmp = input().split()
        rules.append(tmp[0].strip('/').split('/'))
        tags.append(tmp[1])
    
    for i in range(m):
        urls.append(input())
    
    for url in urls:
        flag = False
        output = []
        _idx = 0
        for i in range(len(rules)):
            tmpUrl = url.strip('/')
            idx = 0
            while True:
                if idx >= len(rules[i]):
                    output = []
                    flag = False
                    break
                if tmpUrl.find('/')!=-1 and rules[i][idx]!='<path>':
                    fraction = tmpUrl[0: tmpUrl.find('/')]
                    tmpUrl = tmpUrl[tmpUrl.find('/')+1:]
                else:
                    fraction = tmpUrl
                if fraction.isdigit() and rules[i][idx]=='<int>':
                    output.append(str(int(fraction)))#去掉前导0
                    flag = True
                elif fraction.find('/')==-1  and rules[i][idx]=='<str>':
                    output.append(fraction)
                    flag = True
                elif rules[i][idx]=='<path>':
                    output.append(fraction)
                    flag = True
                elif fraction == rules[i][idx]:
                    #do nothing
                    flag = True
                else:
                    flag = False
                    break
                idx += 1
                if fraction==tmpUrl:
                    break
            if flag:
                _idx = i
                break
        if flag:
            _flag = True
            for ele in output:
                if re.match(r'[a-zA-Z0-9._/]+', ele).group() != ele:
                    print('404')
                    _flag = False
                    break
            if _flag:
                output.insert(0, str(tags[_idx]))
                print(' '.join(output))
        else:
            print('404')
    
    

    相关文章

      网友评论

          本文标题:201803-3(Python实现)

          本文链接:https://www.haomeiwen.com/subject/vsoigctx.html