2011年10月6日 星期四

丁丁共和國

內容 :


丁丁共和國的國民非常喜歡賽車,每個人從小的夢想就是當個賽車選手,國內共有數百個層級不同的賽車聯盟,每天都有十幾場不同類型的賽事進行著,賽車已經成為丁丁共和國的國民運動,自從2080年賽車列入奧運項目後,丁丁共和國永遠都是靠這個項目奪得每次奧運的唯一一面獎牌。





為了慶祝丁丁共和國建國一百年,丁丁總理決定舉辦一場盛大的賽車活動,他決定在國內的高速公路中挑選出一條最長的路徑來舉辦這場賽車,身為體育部長的你必須幫總理挑出這條路徑。





丁丁共和國內有數千個城市,而城市間有一些高速公路連接,但是由於丁丁交通部長的省錢政策,丁丁共和國的高速公路有一個特性,就是任意兩個城市間由高速公路構成的路徑都不多不少恰好為一條,而且連接兩個城市的高速公路長度都是1。


輸入說明 :
輸入檔中會有多筆資料,第一行是一個正整數k,代表一共有多少組資料,接下來是k組測試資料,每組測試資料的第一行是一個正整數n,代表城市的數量,n的大小不會超過2000,接下來每行會有兩個城市名稱,代表這兩個城市間有高速公路連接,城市名稱不會超過100個字元且不含空白,當兩個城市名稱為等號時代表一組測試資料結束,不要把最後一行納入處理。


輸出說明 :
對每組測試資料,輸出一行所能找到的最長賽道長度。


範例輸入 :


2
3
A B
B C
= =
4
A B
B C
B D
= =


範例輸出 :


2
2




出處 :
2007 NPSC 高中組決賽

4 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, K

    For i = 1 To K
    Input #1, N
    List1.Clear
    Do
    Line Input #1, X
    If List1.ListCount = 0 And Left(X, 1) <> "=" Then List1.AddItem Left(X, 1) & Right(X, 1)
    If Left(X, 1) = "=" Then Exit Do
    Call Catch(Left(X, 1), Right(X, 1))
    Loop

    Print #2, max()
    Next i

    Close #2
    Close #1
    End
    End Sub


    Sub Catch(A, B)
    For i = 0 To List1.ListCount - 1
    If Right(List1.List(i), 1) = A Then List1.AddItem List1.List(i) & B
    Next i
    End Sub

    Function max()
    M = 0
    For i = 0 To List1.ListCount - 1
    L = Len(List1.List(i))
    If L > M Then M = L
    Next i
    max = M - 1
    End Function

    in.txt---------------
    4
    3
    A B
    B C
    = =
    4
    A B
    B C
    B D
    = =
    5
    B C
    C D
    D E
    E F
    = =
    5
    B C
    C D
    D E
    D F
    = =

    out.txt--------------
    2
    2
    4

    回覆刪除
  2. 修正:城市名稱不只1位(arror提醒)

    Dim Y() As String

    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Input #1, K

    For i = 1 To K
    Input #1, N
    List1.Clear: List2.Clear: List3.Clear
    Do
    Line Input #1, x
    x = UCase(x)
    Y = Split(x)
    If Y(0) = "=" Then Exit Do
    If List3.ListCount = 0 Then List1.AddItem Y(0): List2.AddItem Y(1): List3.AddItem Y(0) & " " & Y(1)
    Call Catch(Y(0), Y(1), Len(Y(0)))
    Loop

    Print #2, max()
    Next i

    Close #2
    Close #1
    End
    End Sub


    Sub Catch(A, B, LeA)
    For i = 0 To List3.ListCount - 1
    If Right(List3.List(i), LeA) = A Then List3.AddItem List3.List(i) & " " & B
    Next i
    End Sub

    Function max()
    M = 0
    For i = 0 To List3.ListCount - 1
    L = Cal(List3.List(i))
    If L > M Then M = L
    Next i
    max = M
    End Function

    Function Cal(A)
    C = 0
    For i = 1 To Len(A)
    If Mid(A, i, 1) = " " Then C = C + 1
    Next i
    Cal = C
    End Function

    in.txt----------------
    5
    3
    A B
    B C
    = =
    4
    A B
    B C
    B D
    = =
    5
    B C
    C D
    D E
    E F
    = =
    5
    B C
    C D
    D E
    D F
    = =
    6
    Arror Yo
    Yo Go
    Go Happy
    Go Shopping
    Shopping Money
    = =

    out.txt-------------
    2
    2
    4
    3
    4

    回覆刪除
  3. 網誌管理員已經移除這則留言。

    回覆刪除
  4. Dim mm$, x As String, Y$, tmp, times, Ans, m, A, B, U
    Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2

    Input #1, Frt
    For Frts = 1 To Frt
    Input #1, sec
    List1.Clear
    List2.Clear
    List3.Clear
    For i = 1 To sec
    Input #1, mm
    m = Split(mm): x = m(0): Y = m(1)
    List1.AddItem x: List2.AddItem Y
    Next
    For i = 0 To List1.ListCount - 1
    List3.List(i) = List1.List(i) & " " & List2.List(i)
    Next


    Call MySub

    Next


    Close
    Close
    End
    End Sub

    Sub MySub()

    For i = 0 To List1.ListCount - 1
    For j = 0 To List1.ListCount - 1
    If List1.List(j) = List2.List(i) And List2.List(i) <> "=" Then If MMM = "" Then MMM = List2.List(i): List4.AddItem MMM: Exit For Else MMM = MMM & " " & List2.List(i): List4.AddItem MMM: Exit For
    Next
    Next

    Dim U
    U = Split(MMM)

    For i = 0 To List1.ListCount - 1
    If List2.List(i) = U(0) Then MMM = List1.List(i) & " " & MMM: Exit For
    Next

    For i = 0 To List1.ListCount - 1
    If List1.List(i) = U(UBound(U)) Then MMM = MMM & " " & List2.List(i): Exit For
    Next

    Call maXx(MMM)

    End Sub

    Sub maXx(QQQ)


    k = Split(QQQ)
    Ans = UBound(k)
    Print #2, Ans
    End Sub

    回覆刪除