2

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
}
4

6 回答 6

4
public void CopyTo(
    int sourceIndex,
    char[] destination,
    int destinationIndex,
    int count
)
于 2012-08-08T12:15:15.417 回答
0
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';
        }
于 2012-08-09T08:51:36.290 回答
0

用这个:

using (Stream stdin = Console.OpenStandardInput())
{
    byte[] buffer = new byte[1024];
    stdin.Read(buffer, 0, buffer.Length);
    char[] inputCharArray = System.Text.Encoding.Default.GetChars(buffer);
}
于 2012-08-08T12:17:01.353 回答
0

尝试:

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

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

我不确定它是否会提高性能。虽然清楚地解释你需要什么真的很有帮助

于 2012-08-08T12:01:25.403 回答
0

使用StringBuilder.

此代码在大约一秒内处理 100 x 10000 个字符。如果您需要它更快,您可以使用 TPL - 命名空间中的任务并行库使您的任务并行: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();
        }
    }
}
于 2012-08-08T12:24:36.567 回答
0

你可以做

char[] Words = "Exemple string test".ToCharArray();

char[] Words = Console.ReadLine().ToCharArray();

于 2018-12-13T21:44:52.057 回答