日期:2014-05-20  浏览次数:20944 次

超级郁闷,4个方法都“Memory limit exceeded”
题目是这样的:
输入一个字符串,如果这个字符串是由in,input,out,output,puton,one组成,就返回YES,否则返回NO。
时间限制是1秒,内存限制16M。每个字符串长度不超过10^7个字符。


方法一:

C# code
using System;
using System.Text.RegularExpressions;

class Program
{
static void Main(string[] args)
{
int lenNum = int.Parse(Console.ReadLine());
for (int i = 0; i < lenNum; i++)
{
if (Regex.IsMatch(Console.ReadLine(), "^(?:one|out(?:put)?|in(?:put)?|puton)+$")) Console.WriteLine("YES");
else Console.WriteLine("NO");
}
}
}



方法二:
C# code
using System;
class Program
{
static void Main(string[] args)
{
int lenNum = int.Parse(Console.ReadLine());
for (int i = 0; i < lenNum; i++)
{
if (Console.ReadLine().Replace("one","").Replace("puton","").Replace("output","").Replace("input","").Replace("out","").Replace("in","")=="") Console.WriteLine("YES");
else Console.WriteLine("NO");
}
}
}



方法三:
C# code
using System;
class Program
{
static void Main(string[] args)
{
int lenNum = int.Parse(Console.ReadLine());
for (int i = 0; i < lenNum; i++)
Console.WriteLine(Judge(Console.ReadLine()));
}


static string Judge(string input)
{
int pos = 0;
for (int i = input.Length - 1; i >= 0; i--)
{
pos++;
if (pos == 1||pos==4) continue;
else if (pos == 2)
{
if (input.Substring(i, pos) == "in") pos = 0;
else continue;
}
else if (pos == 3)
{
if (input.Substring(i, pos) == "one" || input.Substring(i, pos) == "out") pos = 0;
else continue;
}
else if (pos == 5)
{
if (input.Substring(i, pos) == "input" || input.Substring(i, pos) == "puton") pos = 0;
else continue;
}
else
{
if (input.Substring(i, pos) == "output") pos = 0;
else return "NO";
}
}
if (pos==0) return "YES";
else return "NO";
}
}


方法四:
C# code
using System;
    class Program
    {
        static void Main(string[] args)
        {
            int lenNum = int.Parse(Console.ReadLine());
            for (int i = 0; i < lenNum; i++)
                Console.WriteLine(JudgeInput(Console.ReadLine()));
}

        static string JudgeInput(string input)
        {
            int pos = 0;
            for (int i = input.Length - 1; i >= 0; i--)
            {
                pos++;
                switch (input[i])
                {
                    case 'p':
                        if (pos == 5)
                        {
                            if (input.Substring(i, 5) == "puton") pos = 0;
                            else pos = -1;
                        }
                        break;
                    case 'i': 
                        if (pos==2||pos==5)
                        {
                            if(input.Substring(i, pos) == "in"||input.Substring(i, pos) == "input") pos = 0; 
                            else pos = -1;
                        }
                        else pos=-1;break;
                    case 'o':
                        if (pos == 3)
                        {
                            if (input.Substring(i, pos) == "one" || input.Substring(i, pos) == "out") pos = 0;
                            else pos = -1;
                        }
                        else if (pos == 6)
                        {
                            if (input.Substring(i, 6) == "output") pos = 0;
                            else pos = -1;
                        }
                        break;
                    case 'n':
                    case 'e':
                    case 't':
                    case 'u': break;
                    default: pos = -1; break;
                }
                if (pos < 0 || pos > 7) return "NO";
            }
            if (pos==0) return "YES";
            else return "NO";
        }
}