Kattis问题:电话清单C#

时间:2015-07-11 20:27:42

标签: c# performance stringbuilder

我目前正在学习C#并且一直在做一些Kattis编码问题。现在我陷入电话列表挑战https://kth.kattis.com/problems/phonelist并继续获得“超出时间限制”错误,这意味着运行我的代码需要4秒多的时间。这是我一直在运行的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace phonelist
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();

            // Variables.
            int numTestCases = 0;
            int numPhoneNumbers = 0;

            // Save number of test cases.
            numTestCases = int.Parse(Console.ReadLine());

            // Do this for each test case.
            for (int i = 0; i < numTestCases; i++)
            {
                // Variables.
                List<string> phoneNumbersList = new List<string>();
                string[] phoneNumbersArray = null;
                bool pass = true;

                // Save number of phone numbers.
                numPhoneNumbers = int.Parse(Console.ReadLine());

                // Save all phonenumbers in the list.
                for (int j = 0; j < numPhoneNumbers; j++)
                {
                    string number = Console.ReadLine();

                    // Remove whitespaces.
                    number = ReplaceWhitespaces(number);

                    // Add to list.
                    phoneNumbersList.Add(number);
                }

                // Save list to array.
                phoneNumbersArray = phoneNumbersList.ToArray();

                // Validate each phone number against each other.
                foreach (string phoneNumber in phoneNumbersArray)
                {
                    bool doOnce = true;

                    foreach (string otherNumber in phoneNumbersArray)
                    {
                        // Skip iteration if it finds one occurance of itself.
                        if(phoneNumber.Equals(otherNumber) && doOnce)
                        {
                            doOnce = false;
                            continue;
                        }

                        // Check if the string is found inside the other strings.
                        int firstOccuranceIndex = otherNumber.IndexOf(phoneNumber);

                        // If it's found at the first position...
                        if (firstOccuranceIndex == 0)
                        {
                            pass = false;
                        }
                    }
                }

                // Write output.
                switch (pass)
                {
                    case(true):
                        Console.WriteLine("YES");
                        break;
                    case (false):
                        Console.WriteLine("NO");
                        break;
                }
            }
        }

        // Replace whitespaces using Stringbuilder.
        private static string ReplaceWhitespaces(string data)
        {
            StringBuilder sb = new StringBuilder(data.Length);

            // Loop through each character.
            for (int i = 0; i < data.Length; i++)
            {
                char c = data[i];

                // If not a whitespace, append to stringbuilder.
                if(!char.IsWhiteSpace(c))
                {
                    sb.Append(c);
                }
            }

            return sb.ToString();
        }
    }
}

现在我一直在阅读StringBuilder类,并认为问题在于我如何消除空格,但我无法绕过我做错的事情。有没有更合适的方法来优化我的程序性能?

2 个答案:

答案 0 :(得分:0)

如果空格是“”,你不能在字符串上使用Replace(“”,“”)吗?

答案 1 :(得分:0)

试试这段代码:

class Program
{
    static void Main(string[] args)
    {
        int t, n;
        bool isConsistent;
        string s;
        t = int.Parse(Console.ReadLine().Trim());
        for (int i = 0; i < t; i++)
        {
            isConsistent = true;
            n = int.Parse(Console.ReadLine().Trim());
            int count = 0;
            while (isConsistent && count < n)
            {
                s = Console.ReadLine().Trim();
                if (s.StartsWith("911") && s.Length > 3)
                {
                    isConsistent = false;                       
                }
                count++;
            }
            if (isConsistent) Console.WriteLine("YES");
            else Console.WriteLine("NO");
        }
    }
}

正如@Sami Kuhmonen所提到的,您不需要存储每个数字并检查每个输入。如果您已经拥有以911开头的电话号码,那么您可以停止所有检查。 因此,内部while循环会一直运行,直到isConsistent不成为false或直到所有数字都没有输入为止。 希望这会有所帮助。