2011年11月8日 星期二

99正式 Problem 3:字串特徵的判斷

此問題為給定某些文句,請選手判斷這些文句是否符合某些檢查條件?


本子題給定的文句內容只有「英文、數字、空白及標點符號」,並且「沒有」任何全型字。文句中使用的「標點符號」只包括「,」及「.2種。我們定義一個「表示式」,是一個包含若干個「表示符號」的字串。而本題的「表示符號」只包括「 #」、「*」和「$3種。其中, 1個「#」表示 1個「0~9」的數字。另, 1個「*」表示 1個大寫的英文字母,而 1個「$」表示 1個小寫的英文字母。請選手在檢查文句中,找尋是否存在符合某個給定「表示式」的子字串。例如:有個「表示式」為「###**$」,表示為「連續 3個數字,緊接著連續 2個大寫英文字,其後緊接著 1個小寫英文字」。




輸入說明:第 1行表示欲檢查的「表示式」,其長度不超過 20個字。第 2~5行表示待檢查的 4句英文文句,每行文句均獨立檢查,每句文句不超過 100個字。


輸出說明:產生 4行輸出,依序表示輸入檔第 2~5行英文文句的檢查結果。若檢查的文句中存在符合「表示式」的子字串,請輸出「符合」,否則輸出「不符合」。



輸入檔案 1:【檔名:in1.txt】
#***$$
Its password is aXYZ3KBGna.
M16A is a code of a machine gun.
Your employee number is 5AAAbb.
The serial number AQ773276UKD was printed in this case.


輸入檔案 2:【檔名:in2.txt】
*#$#
Its password is M5k3.
This serial number is 63M43Kk.
Her employee number is 8J3m75.
Those item numbers are AQ43 and KM4M.


輸出檔案:【檔名:out.txt】

符合
不符合
符合
不符合


符合
不符合
符合
不符合

2 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\in2.txt" For Input As #2
    Open App.Path & "\out.txt" For Output As #3

    '----------------in1.txt-------------------
    Line Input #1, X

    Do While Not EOF(1)

    Line Input #1, Y
    OK = False
    For i = 1 To (Len(Y) - Len(X) + 1)
    A = Mid(Y, i, Len(X))

    P = True
    For j = 1 To Len(X)
    B = Mid(X, j, 1)
    C = Mid(A, j, 1)
    Select Case B
    Case "#": If check_1(C) = False Then P = False
    Case "*": If check_2(C) = False Then P = False
    Case "$": If check_3(C) = False Then P = False
    End Select
    Next j
    If P = True Then OK = True

    Next i

    If OK = True Then Print #3, "符合" Else Print #3, "不符合"
    Loop

    '-----------------------in2.txt------------------------------
    Print #3,
    Line Input #2, X

    Do While Not EOF(2)

    Line Input #2, Y
    OK = False
    For i = 1 To (Len(Y) - Len(X) + 1)
    A = Mid(Y, i, Len(X))

    P = True
    For j = 1 To Len(X)
    B = Mid(X, j, 1)
    C = Mid(A, j, 1)
    Select Case B
    Case "#": If check_1(C) = False Then P = False
    Case "*": If check_2(C) = False Then P = False
    Case "$": If check_3(C) = False Then P = False
    End Select
    Next j
    If P = True Then OK = True

    Next i

    If OK = True Then Print #3, "符合" Else Print #3, "不符合"
    Loop


    Close #3
    Close #2
    Close #1
    End
    End Sub

    Function check_1(A) '0~9
    P = False
    If A >= 0 And A <= 9 Then P = True
    check_1 = P
    End Function

    Function check_2(A) ' A~Z
    P = False
    If A >= "A" And A <= "Z" Then P = True
    check_2 = P
    End Function

    Function check_3(A) ' a~z
    P = False
    If A >= "a" And A <= "z" Then P = True
    check_3 = P
    End Function

    回覆刪除
  2. Dim s
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in1.txt" For Input As #1
    Open App.Path & "\in2.txt" For Input As #2
    Open App.Path & "\out.txt" For Output As #3

    Input #1, r
    For i = 1 To 4
    Line Input #1, n
    Call A1(n, r)
    Next
    Print #3, " "
    Input #2, r
    For i = 1 To 4
    Line Input #2, n
    Call A1(n, r)
    Next


    Close
    Close
    Close
    End
    End Sub

    Sub A1(a, b)
    Dim m$
    s = Split(a)
    For i = 0 To UBound(s)
    If Len(s(i)) >= Len(b) Then

    For j = 1 To Len(s(i)) - Len(b)
    m = Mid(s(i), j, Len(b))
    ch = 1
    For L = 1 To Len(b)
    Q1 = Mid(m, L, 1)
    Q2 = Mid(b, L, 1)
    Select Case Q2
    Case "#": If Q1 > "9" Or Q1 < "0" Then ch = 0: Exit For
    Case "*": If Q1 > "Z" Or Q1 < "A" Then ch = 0: Exit For
    Case "$": If Q1 > "z" Or Q1 < "a" Then ch = 0: Exit For
    End Select
    Next
    If ch = 1 Then Print #3, "符合": Exit Sub
    Next
    End If
    Next
    Print #3, "不符合"
    End Sub

    回覆刪除