2012年11月20日 星期二

密碼分析

密碼分析(cryptanalysis)是指把某個人寫的密文加以分解。這個程序通常會對密文訊息做統計分析。你的任務就是寫一個程式來對密文作簡單的分析。
輸入規範
輸入檔案的第一列有一個正整數n,代表以下有多少列需要分析的密文。接下來的n列,每列含有0或多個字元(可能包含空白字元)
輸出規範
每列包含一個大寫字元(A~Z)和一個正整數。這個正整數代表該字元在輸入檔案中出現的次數。輸入中大小寫(例如:A及a)視為相同的字元。輸出時請按照字元出現的次數由大到小排列,如果有2個以上的字元出現次數相同的話,則按照字元的大小(例如:A在H之前)由小到大排列。
請注意:如果某一字元未出現在輸入檔案中,那它也不應出現在輸出檔案中。
輸入範例(test5.txt)
3
This is a test.
Count me 1 2 3 4 5.
Wow!!!! Is this question easy?
輸出範例(result5.txt)
S 7
T 6
I 5
E 4
O 3
A 2
H 2
N 2
U 2
W 2
C 1
M 1
Q 1
Y 1

2 則留言:

  1. Dim word(26) As Integer
    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, mycount
    For i = 1 To mycount
    Input #1, temp
    strr = strr & temp
    Next
    For ii = 65 To 90
    ori = Len(strr)
    strr = Replace(strr, Chr(ii), "")
    strr = Replace(strr, Chr(ii + 32), "")
    word(ii - 64) = ori - Len(strr)
    If word(ii - 64) <> 0 Then List1.AddItem word(ii - 64) - 100000
    Next
    For iii = 0 To List1.ListCount
    For iiii = 1 To 26
    If Val(List1.List(iii)) + 100000 = word(iiii) Then Print #2, Chr(iiii + 64) & " " & word(iiii): List1.RemoveItem iii
    Next
    Next
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  2. Dim a(26), k, c() As Integer
    Dim b() 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, n
    List1.Clear
    For w = 1 To Val(n)
    Line Input #1, x
    x = UCase(x)
    For i = 1 To Len(x)
    If Asc(Mid(x, i, 1)) >= 65 And Asc(Mid(x, i, 1)) <= 90 Then a(Asc(Mid(x, i, 1)) - 64) = a(Asc(Mid(x, i, 1)) - 64) + 1
    Next i
    Next w
    For i = 1 To 26
    If a(i) = "" Then a(i) = 0
    List1.AddItem a(i) & Chr(i + 64)
    Next i
    k = 1
    For i = List1.ListCount - 1 To 0 Step -1
    If Left(List1.List(i), 1) <> 0 Then
    ReDim Preserve b(k), c(k)
    c(k) = Left(List1.List(i), Len(List1.List(i)) - 1): b(k) = Right(List1.List(i), 1)
    k = k + 1
    End If
    Next i
    For i = 1 To UBound(b)
    For j = i + 1 To UBound(b)
    If c(i) < c(j) Then g = b(i): b(i) = b(j): b(j) = g: h = c(i): c(i) = c(j): c(j) = h
    If c(i) = c(j) And b(i) > b(j) Then g = b(i): b(i) = b(j): b(j) = g: h = c(i): c(i) = c(j): c(j) = h
    Next j
    Next i
    For i = 1 To UBound(b)
    Print #2, b(i) & " " & c(i)
    Next i
    Close #2
    Close #1
    End
    End Sub

    回覆刪除