ブルーブリッジカップALGO-88文字列のアルゴリズムのトレーニング統計

文字列のアルゴリズムのトレーニング統計  

制限時間:メモリ制限が1.0S:512.0メガバイト

 

問題の説明

  長さN Sのストリング所与、数Lがある(別の発生を交差することができる)、複数存在する場合、最長の出力がある場合、まだサブストリングLの発生の統計的な頻度よりもほとんど等しい長さより大きいであります初めて早いため、より出力。

 

入力形式

  最初の桁ラインL.
  2行目は、文字列Sです。
  Lは長S.よりも0より大きくはありません

 

出力フォーマット

  ライン、要求文字列の対象。

  入力サンプル1:
  4
  bbaabbaaaaaの

  出力例1:
  BBAAの

  入力サンプル2:
  2
  bbaabbaaaaaの

  出力サンプル2:
  AA

 

スケールデータと規則

  N- <60 =
  S、すべての文字は小文字です。

 

プロンプト

  すべての可能な部分文字列、統計的な出現、その資格を見つけるための列挙

 

#include <stdio.h>
#include <string.h>

int main()
{
    char S[65] = { 0 };
    int L, len;

    scanf("%d", &L);
    scanf("%s", S);
    len = (int)strlen(S);

    int current_start, current_end, current_times, max_start, max_end, max_times = -1, equal;
    for (int i = 0; i < len - L + 1; ++i)
    {
        for (current_start = i, current_end = current_start+L-1; current_end < len; ++current_end)
        {
            current_times = 1;
            for (int p = current_start+1, q = current_end+1; q < len; ++p, ++q)
            {
                equal = 1;
                for (int k = 0; k < current_end - current_start + 1; ++k)
                {
                    if (S[current_start + k] != S[p + k])
                    {
                        equal = 0;
                        break;
                    }
                }
                if (equal)
                    current_times++;
            }
            if (current_times > max_times)
            {
                max_times = current_times;
                max_start = current_start;
                max_end = current_end;
            }
            else if (current_times == max_times && (current_end - current_start + 1) > (max_end - max_start + 1))
            {
                max_times = current_times;
                max_start = current_start;
                max_end = current_end;
            }
        }
    }
    for (int p = max_start; p <= max_end; ++p)
        printf("%c", S[p]);

    return 0;
}

 

公開された221元の記事 ウォン称賛40 ビュー40000 +

おすすめ

転載: blog.csdn.net/liulizhi1996/article/details/103995707