编程挑战:时间限制除外 [英] Programming Challenge: Time limit exceded

查看:58
本文介绍了编程挑战:时间限制除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!我正在做一个编程挑战,挑战是:



给定一个电话号码列表,确定它是否一致,因为没有数字是另一个号码的前缀。假设电话目录列出了这些数字:



紧急911

Alice 97 625 999

Bob 91 12 54 26

在这种情况下,无法给Bob打电话,因为只要您拨打了Bob的电话号码的前三位数字,中心就会将您的呼叫转到紧急线路。所以这个列表不一致。



输入

第一行输入给出一个整数,1≤t≤40,测试用例数量。每个测试用例以单独一行的电话号码n开始,1≤n≤10000。然后跟随n行,每行有一个唯一的电话号码。电话号码是最多十位数的序列。



输出

对于每个测试用例,如果列表是,则输出YES一致,否则为否。



样本输入:

3

911

97625999

91125426

5

113

12340

123440

12345

98346



样品输出:

NO





我的代码是:

 公开 计划
{
静态列表< string> testList = new List< string>();

静态 void Main( string [] args)
{
validateTestCases(getAllPhoneNumbers());
}

public static 列表< string> getAllPhoneNumbers()
{
List< string> phoneList = new List< string>();
string 输入;
while ((input = Console.ReadLine())!= null && input!=
{
phoneList.Add(input);
}
return phoneList;
}

public static void validateTestCases(List< string> inputList)
{
int numbersOfPhoneNumbers;
for int i = 1 ; i < inputList.Count; i ++)
{
numbersOfPhoneNumbers = Convert.ToInt32(inputList [i]);
for int j = i + 1 ; j < = i + numbersOfPhoneNumbers; j ++)
{
testList.Add(inputList [j]);
if (j ==(i + numbersOfPhoneNumbers))
{
checkPrefixes(testList);
}
}
i = i + numbersOfPhoneNumbers;
}
}


public static void checkPrefixes(List< string> input)
{
for int i = 0 ; i < input.Count; i ++)
{
for int j = 0 ; j < input.Count; j ++)
{
if (input [i] .StartsWith(input [j])&& i!= j)
{
Console.WriteLine( NO);
testList.Clear();
return ;
}
}
}
testList.Clear();
Console.WriteLine( YES);
}

}





答案似乎是对的,但我的灵魂超过时间限制女巫是4秒?我怎样才能让它跑得更快?会粘贴主要帮助中的所有代码吗?赞美所有的Anwsers! :)

解决方案

这是一个编码挑战 - 一场比赛。

这意味着你可以找到一个有效且适合的解决方案挑战的参数。



我们告诉你做什么对任何其他人都不公平,是吗?这应该是关于你的编码能力,而不是我们的编码能力!这是你第三次尝试获得挑战解决方案......



自己尝试一下:它可能会让你成为比我们提供解决方案更好的程序员。 ..

Hello! Im doing a programming challenge and the challenge is:

Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:

Emergency 911
Alice 97 625 999
Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

Input
The first line of input gives a single integer, 1≤t≤40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1≤n≤10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

Output
For each test case, output "YES" if the list is consistent, or "NO" otherwise.

Sample input:
3
911
97625999
91125426
5
113
12340
123440
12345
98346

Sample output:
NO
YES

My code is:

public class Program
   {
     static List<string> testList = new List<string>();

       static void Main(string[] args)
       {
           validateTestCases(getAllPhoneNumbers());
       }

       public static List<string> getAllPhoneNumbers()
       {
          List<string> phoneList = new List<string>();
           string input;
           while ((input = Console.ReadLine()) != null && input != "")
           {
               phoneList.Add(input);
           }
           return phoneList;
       }

       public static void validateTestCases(List<string> inputList)
       {
           int numbersOfPhoneNumbers;
           for (int i = 1; i < inputList.Count; i++)
           {
               numbersOfPhoneNumbers = Convert.ToInt32(inputList[i]);
               for (int j = i + 1; j <= i + numbersOfPhoneNumbers; j++)
               {
                   testList.Add(inputList[j]);
                   if (j == (i + numbersOfPhoneNumbers))
                   {
                       checkPrefixes(testList);
                   }
               }
               i = i + numbersOfPhoneNumbers;
           }
       }


       public static void checkPrefixes(List<string> input)
       {
           for (int i = 0; i < input.Count; i++)
           {
               for (int j = 0; j < input.Count; j++)
               {
                   if (input[i].StartsWith(input[j]) && i != j)
                   {
                       Console.WriteLine("NO");
                       testList.Clear();
                       return;
                   }
               }
           }
           testList.Clear();
           Console.WriteLine("YES");
       }

   }



The answser seems to be right but my soultion excedes the time limit witch is 4 secounds? How can i make it run faster? Would pasting in all the code in the main help? Appriciate all Anwsers! :)

解决方案

This is a coding challenge - a competition.
That means it's up to you to find a solution which works and fits the parameters of the challenge.

Us telling you what to do isn't fair on any others trying, is it? This is supposed to be about your coding abilities, not ours! And this is your third try to get challenge solutions...

Give it a try yourself: it might make you a better programmer than if we handed you a solution...


这篇关于编程挑战:时间限制除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆