在大量子字符串中分割大字符串的最佳方法

时间:2016-03-14 10:48:12

标签: string c#-4.0 split substring

我想将一个大字符串文件拆分成大量非常小的子字符串然后存储它,以便我可以在我的程序中进一步使用所有子字符串。什么是最好的方法。我的程序在C#中所以请仅在C#中提供解决方案。

这是输入文件:https://he-s3.s3.amazonaws.com/media/hackathon/code-monk-hashing/problems/monk-in-the-land-of-pokemons/4326354c-2-input-4326316.txt?Signature=mtbhaQ2%2F6VRAH8A9OfZTSAX6oTQ%3D&Expires=1457955770&AWSAccessKeyId=AKIAJLE6MUHDYS3HN6YQ

我希望所有数字都能通过集合中的空格分隔。 我可以读取由空格分隔的所有数字而不将它们保存在集合中吗?

问题: 僧侣必须访问一片土地,在那里奇怪的生物,被称为小宠物,在野外漫游。这片土地上的每个神奇宝贝都会攻击任何游客。他们只能通过喂他们喜欢的食物来平息。

X型神奇宝贝吃一种X型食物。

Monk知道他将在途中遇到N个池塘。在每个池塘,他会找到一个食物,然后遇到一个口袋妖怪。第i个池塘有Ai型食品和Bi型口袋妖怪。

如果类型匹配,僧侣可以将第i个池塘的物品送到池塘的口袋妖怪。在离开之前,和尚可能不得不随身携带一些食物,以便喂养所有的小宠物。帮助他找到必须携带的物品数量,以便能够安全地穿过这片土地。

输入: 第一行包含T,表示测试用例的数量。然后,T测试案例随之而来。 每个测试用例的第一行包含一个整数N.然后,N行跟随。 每行由2个空格分隔的整数Ai和Bi组成。

输出:对于每个测试用例,请在新行中打印答案。

约束: 1≤T≤10 1≤N≤105 1≤Ai,Bi≤106

代码:



using System;
using System.Numerics;
using System.Collections.Generic;
class MyClass
{
    static void Main(string[] args)
    {
        int TestCaseNo = Int32.Parse(System.Console.ReadLine().Trim());
        int TestCase;
        for (int j = 0; j < TestCaseNo; j++)
        {
            LinkedList<int> numbers = new LinkedList<int>();
            numbers.Clear();
            int count = 0;
            TestCase = Int32.Parse(System.Console.ReadLine().Trim());

            for (int i = 0; i < TestCase; i++)
            {

                string line = System.Console.ReadLine().Trim();
                string[] temp = line.Split(' ');
                if (Int32.Parse(temp[0]) == Int32.Parse(temp[1]))
                { }
                else
                {
                    if (numbers.Contains(Int32.Parse(temp[1])))
                    {
                        numbers.Remove(Int32.Parse(temp[1]));
                        numbers.AddFirst(Int32.Parse(temp[0]));
                    }
                    else
                    {
                        numbers.AddFirst(Int32.Parse(temp[0]));
                        count++;
                    }

                }
            }
            System.Console.WriteLine(count);
        }
    }
}
&#13;
&#13;
&#13;

0 个答案:

没有答案
相关问题