「電話番号の要素を求める」っぽい事を英語でのたまっておられるようですが、よくわからん。
例えて言うならツリーの枝の長さを求めるみたいな……。
やればわかる。多分!
www.codingame.com
for文の中がこちゃこちゃっとしてて、もう少し綺麗にできそう。
私は「動けばいいや」タイプなのでコレで勘弁してください。
using System; using System.Linq; using System.IO; using System.Text; using System.Collections; using System.Collections.Generic; /** * Auto-generated code below aims at helping you parse * the standard input according to the problem statement. **/ class Solution { static void Main(string[] args) { //電話番号の数 int N = int.Parse(Console.ReadLine()); //番号を格納する配列 string[] tel = new string[N]; //番号の取得 for (int i = 0; i < N; i++) { tel[i] = Console.ReadLine(); } //辞書順(昇順?)ソート Array.Sort(tel, (order1, order2) => order1.CompareTo(order2)); //チェック用 for (int i = 0; i < N; i++) { Console.Error.WriteLine(tel[i]); } //最初の番号の長さをカウンタに入れる int count = tel[0].Length; for (int i = 1; i < N; i++) { //番号の短い方を選ぶ int s = isShorter(tel[i - 1], tel[i]); for (int j = 0; j < s; j++) { //番号違いを発見 if (tel[i - 1][j] != tel[i][j]) { //違う場所以降の長さをカウンタに加えてbreak count += (tel[i].Length - j); break; } //最後まで来た(短い方に違いが無かった場合) if (j == s - 1) { //長さの差を加える count += (tel[i].Length - (j + 1)); } } } // Write an action using Console.WriteLine() // To debug: Console.Error.WriteLine("Debug messages..."); Console.WriteLine(count); // The number of elements (referencing a number) stored in the structure. } //短いほうの文字列の長さを返す static int isShorter(string s1, string s2) { if (s1.Length <= s2.Length) return s1.Length; else return s2.Length; } }