結果

問題 No.628 Tagの勢い
ユーザー yuki2006yuki2006
提出日時 2018-01-11 12:41:27
言語 C#(csc)
(csc 3.9.0)
結果
RE  
実行時間 -
コード長 4,150 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 112,672 KB
実行使用メモリ 25,920 KB
最終ジャッジ日時 2024-04-16 20:04:03
合計ジャッジ時間 2,147 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;

namespace lecture
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            string row = Console.ReadLine();
            int N = int.Parse(row);
            Dictionary<string, int> tagScores = new Dictionary<string, int>();

            for (int i = 0; i < N; i++)
            {
                // 画像の番号の行を読み込む
                string imageNo = Console.ReadLine();

                // タグの数 スコアの行を読み込む
                row = Console.ReadLine();
                // Split() は引数がないと空白で区切って配列にする
                string[] ss = row.Split();

                // 配列の0番目がタグの数
                int tagNum = int.Parse(ss[0]);
                // 配列の1番目がスコア
                int score = int.Parse(ss[1]);

                // タグの行を読み込んで 空白で区切るとタグの配列になる
                string[] tagsTmp = Console.ReadLine().Split();
                // tags[0]=run,tags[1]=getaway ,tags[2]=fast


                //TODO まとめる処理をする
                // それぞれのタグの点数を加算していくだけ
                for (int m = 0; m < tagsTmp.Length; m++)
                {
                    string tag = tagsTmp[m];
                    if (tagScores.ContainsKey(tag))
                    {
                        // Dictionaryにtags[m]の文字列のキーが有る場合
                        // すでにあるキーにスコアを追加してあげる
                        // すでにキーが有る場合は += で追加する
                        tagScores[tag] += score;
                    }
                    else
                    {
                        // キーがない場合は そのスコア作る
                        // 作る場合は =で 代入する  
                        tagScores[tag] = score;
                    }
                }
            }
            // ここまでで、タグとスコアの値を合計できるはず
            // tagScores.Countはそのキーの数なのでその分配列を確保する
            // タグの名前とそのタグのスコア分の配列を用意する
            string[] tags = new string[tagScores.Count];
            int[] scores = new int[tagScores.Count];

            // Dictonaryから一旦配列に入れ直す
            int p = 0;
            foreach (string key in tagScores.Keys)
            {
                tags[p] = key;
                scores[p] = tagScores[key];
                p++;
            }

            // ☆ スコア順 スコアが同じ時にアルファベット順にする処理をする
            // スコアに対してバブルソートをする 添字が小さいほど大きくしたい
            for (int i = 0; i < scores.Length; i++)
            {
                for (int k = scores.Length - i - 1; k >= 0; k--)
                {
                    // string のA.CompareTo(B) は
                    // 戻り値が -1のときは A < B
                    // 戻り値が 0 のときは A == B
                    // 戻り値が 1 のときは A > B
                    if (scores[k] < scores[k + 1] ||
                        scores[k] == scores[k + 1] && tags[k].CompareTo(tags[k + 1]) == 1)
                    {
                        int tmp = scores[k];
                        scores[k] = scores[k + 1];
                        scores[k + 1] = tmp;

                        string tmp2 = tags[k];
                        tags[k] = tags[k + 1];
                        tags[k + 1] = tmp2;
                    }
                }
            }
            // TODO 最大10件まで出力する
            // それらを出力する
            int count = 10;
            if (count > tags.Length)
            {
                count = tags.Length;
            }


            for (int i = 0; i < count; i++)
            {
                Console.WriteLine(tags[i] + " " + scores[i]);
            }

        }
    }
}
0