zoukankan      html  css  js  c++  java
  • 微软2014校招笔试题-String reorder

    Time Limit:10000ms
    Case Time Limit:1000ms
    Memory Limit:256MB

    Description

    For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).

    Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
    1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
    2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.

    Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).


    Input


    Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.

    Output


    For each case, print exactly one line with the reordered string based on the criteria above.

    Sample Input
    aabbccdd
    007799aabbccddeeff113355zz
    1234.89898
    abcdefabcdefabcdefaaaaaaaaaaaaaabbbbbbbddddddee
    Sample Output
    abcdabcd
    013579abcdefz013579abcdefz
    <invalid input string>
    abcdefabcdefabcdefabdeabdeabdabdabdabdabaaaaaaa



    我的水平有限,但最终也解答出来了。下面贴出个人的答案,欢迎大牛批评指正。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace StringReorder
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                string input = string.Empty;
                input = Console.ReadLine();
    
                GetResults(input);
    
            }
    
            static void GetResults(string input)
            {
                string results = "";
                if (MatchRequirement(input))
                {
                    List<char> inputCharList = sort(input).ToList();
                    List<char> tempList = new List<char>();
                    while (inputCharList.Count > 0)
                    {
    
                        tempList.Add(inputCharList[0]);
                        inputCharList.RemoveAt(0);
                        int i = 0;
                        while (i < inputCharList.Count)
                        {
                            if (inputCharList[i] > tempList[tempList.Count - 1])
                            {
                                tempList.Add(inputCharList[i]);
                                inputCharList.RemoveAt(i);
                            }
                            else
                            {
                                i++;
                            }
    
                        }
                        results += ListToString(tempList);
                        tempList.Clear();
                    }
                    Console.WriteLine(results);
                }
            }
    
            static string ListToString(List<char> tempList)
            {
                string tempStr = "";
                for (int i = 0; i < tempList.Count; i++)
                {
                    tempStr += tempList[i];
                }
                return tempStr;
            }
            static string sort(string inputString)
            {
                char[] inputChars = inputString.ToArray();
                string tempStr = "";
                Array.Sort(inputChars);
                for (int i = 0; i < inputChars.Length; i++)
                {
                    tempStr += inputChars[i];
                }
                return tempStr;
            }
            static bool MatchRequirement(string inputString)
            {
                bool tempValue = false;
                char[] inputChars = inputString.ToArray();
    
                for (int i = 0; i < inputChars.Length; i++)
                {
                    if (!match(inputChars[i]))
                    {
                        Console.WriteLine("<invalid input string>");
                        tempValue = false;
                        break;
                    }
                    else
                    {
                        tempValue = true;
                    }
    
                }
                return tempValue;
            }
            static bool match(char c)
            {
                if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))
                {
                    return true;
                }
                return false;
            }
        }
    }
    


  • 相关阅读:
    第八节:layout和partialView、RenderPartial和Partial、 action和RenderAction
    第七节:HtmlHelper及其扩展
    第六节:Razor引擎及相关语法
    第五节:从源码的角度理解各种Result(ActionResult、JsonResult、JavaScriptResult等)
    图片保存到数据库以及C#读取图片
    C#复制数据到剪切板
    IIS8.5支持WCF
    C# 中const和readonly的区别
    C#获取类库(DLL)的绝对路径
    C#怎样用文件读写在文件的原有基础上追加一行数据
  • 原文地址:https://www.cnblogs.com/mantgh/p/3661421.html
Copyright © 2011-2022 走看看