问题描述:

I need to read a string from standard input, perform some operations and meanwhile I need to copy some of input to char array.

In C++ I could do it like this:

scanf("%s", &array[pos]);

which copies string s to char array at position pos.

I need to do do this very quickly (olympic task).

Reading from one big while in C++ took 5 sec.

On C# I tried copying to array using string.elementAt() in a loop but it took 70 seconds, which is way too much. Also, building one big string and than using string.ToCharArray() is a bad idea.

Any ideas how to do that?

char[] ciag = new char[1010001];

for(int x = 0 ; x < n ; x++){

line = Console.ReadLine();

sekw[x] = poz;

len = int.Parse(line.Split(' ')[0]); //length of string to copy

string znaki = line.Split(' ')[1]; //copied string

for (int j = 0; j < len; j++)

{

ciag[poz + j] = znaki[j]; //put into array. Perhaps slow.

}

poz += len;

ciag[poz++] = 'k'; //my stuff

}

网友答案:
public void CopyTo(
    int sourceIndex,
    char[] destination,
    int destinationIndex,
    int count
)
网友答案:

try:

char[] c = new char[stringa.Length];   

for(int i = 0;i<stringa.Length;i++){
     c[i] = stringa[i];
}

I'm not really sure it will improve the performance though. Although clearly explaining what you need would really help

网友答案:

Use this:

using (Stream stdin = Console.OpenStandardInput())
{
    byte[] buffer = new byte[1024];
    stdin.Read(buffer, 0, buffer.Length);
    char[] inputCharArray = System.Text.Encoding.Default.GetChars(buffer);
}
网友答案:

Use a StringBuilder.

This code processes 100 x 10000 characters in about one second. If you need it faster you can make your tasks parallel using the TPL - Task Parallel Library from namespace: System.Threading.Tasks

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] input = CreateInput();
            DateTime start = DateTime.Now;
            for (int i = 1; i < 100; i++)
            {
                StringCutter(new String(input));
            }
            Console.WriteLine((DateTime.Now - start).ToString());
            Console.ReadKey();
        }

        private static char[] CreateInput()
        {
            const int len = 10000;
            char[] ret = new char[len];
            for (int i = 0; i < len; i++)
            {
                ret[i] = Convert.ToChar(i % 256);
            }
            return ret;
        }

        private static String StringCutter(String str)
        {
            List<char> exclusion = new List<char> { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' };
            StringBuilder result = new StringBuilder(str);
            for (int i = 0; i < result.Length; i++)
            {
                if (!(exclusion.Contains(result[i])))
                {
                    result.Remove(i, 1);
                    i--;
                }
            }
            return result.ToString();
        }
    }
}
网友答案:
for (int x = 0; x < n; x++)
        {
            line = Console.ReadLine();
            sekw[x] = poz;
            len = int.Parse(line.Split(' ')[0]);
            char[] temp = line.Split(' ')[1].ToCharArray();
            **Array.Copy(temp, 0, ciag, poz, temp.Length);** 
            poz += len;
            ciag[poz++] = 'k';
        }
相关阅读:
Top