結果

問題 No.628 Tagの勢い
ユーザー yuki2006yuki2006
提出日時 2018-01-11 14:41:01
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 4,179 bytes
コンパイル時間 880 ms
コンパイル使用メモリ 105,856 KB
実行使用メモリ 19,456 KB
最終ジャッジ日時 2024-04-16 20:04:39
合計ジャッジ時間 2,204 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 class TagScore
        {
            public string tag;
            public int score;
        }
        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

                // それぞれのタグの点数を加算していくだけ
                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はそのキーの数なのでその分配列を確保する
            // タグの名前とそのタグのスコア分の配列を用意する

            TagScore[] tagScoreArray = new TagScore[tagScores.Count];

            // Dictonaryから一旦配列に入れ直す
            int p = 0;
            foreach (string key in tagScores.Keys)
            {
                TagScore t = new TagScore();
                t.tag = key;
                t.score = tagScores[key];

                tagScoreArray[p] = t;
                p++;
            }

            Array.Sort(tagScoreArray, ScoreSort);

            // 最大10件まで出力する
            int count = 10;
            if (count > tagScoreArray.Length)
            {
                count = tagScoreArray.Length;
            }

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

        }
        // x , y という順で引数が与えられる。
        // xのほうが先に来てほしい場合 戻り値は -1
        // 逆の場合は        戻り値は 1     を返す
        // あまり気にしなくてもいいんですが 同じ場合は0を返す
        public static int ScoreSort(TagScore x, TagScore y)
        {
            if (x.score > y.score)
            {
                return -1;
            }
            else if (x.score < y.score)
            {
                return 1;
            }
            else
            {
                // x.score == y.scoreのとき
                if (x.tag.CompareTo(y.tag) == -1)
                {
                    return -1;
                }
                else if (x.tag.CompareTo(y.tag) == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
        }
    }
}
0