C#练习题答案: 巴路士惠勒改造【难度:4级】--景越C#经典编程题库,1000道C#基础练习题等你来挑战

巴路士惠勒改造【难度:4级】:

答案1:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
           if(s == "") return new Tuple<string, int>("", 0);
           var mat = Enumerable.Range(0,s.Length).Select((_,i) => s.Substring(s.Length-i, i) + s.Substring(0,s.Length-i)).OrderBy(e=>e, StringComparer.Ordinal);
           return new Tuple<string, int>(String.Join("",mat.Select(e=>e[e.Length-1])), mat.ToList().IndexOf(s));
        }

        public static string Decode(string s, int idx)
        {
           if(s == "") return "";
           var mat = Enumerable.Range(0,s.Length).Select(_ => "");
           for(int it=0; it<s.Length; it++) mat = mat.Select((e,i) => s[i]+e).OrderBy(e=>e, StringComparer.Ordinal);
           return mat.ElementAt(idx);
        }
    }

答案2:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
        if(string.IsNullOrEmpty(s)) return Tuple.Create("",0);
            string[] matrix = new string[s.Length];
            for (int i = 0; i < s.Length; i++)
            {
                matrix[i] = s.Substring(i, s.Length - i) + s.Substring(0, i);
            }
            matrix = matrix.OrderBy(x => x, StringComparer.Ordinal).ToArray();
            return Tuple.Create(new string(matrix.Select(x => x.Last()).ToArray()), Array.IndexOf(matrix, s));
        }

        public static string Decode(string s, int i)
        {
            if(string.IsNullOrEmpty(s)) return "";
            string[] matrix = new string[s.Length];
            for (int k = 0; k < s.Length; k++)
            {
                for (int j = 0; j < s.Length; j++)
                {
                    matrix[j] = s[j].ToString() + matrix[j];
                }
                matrix = matrix.OrderBy(x => x, StringComparer.Ordinal).ToArray();
            }

            return matrix[i];
        }
    }

答案3:

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

public class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
          if (string.IsNullOrEmpty(s))
          {
              return new Tuple<string, int> (string.Empty, 0);
          }
          
          var primary_index = 0;
          var buffer_in = Encoding.UTF8.GetBytes(s);
          var buffer_out = new byte[buffer_in.Length];
          var size = buffer_in.Length;
        
          int[] indices = new int[size];
          for (var i = 0; i < size; i++)
          {
              indices[i] = i;
          }
        
          Array.Sort(indices, 0, size, new BWTComparator(buffer_in, size));
        
          for (int i = 0; i < size; i++)
          {  
              buffer_out[i] = buffer_in[(indices[i] + size - 1) % size];
          }
          
          for (int i = 0; i < size; i++)
          {
            if (indices[i] == 0)
            {
              primary_index = i;
              break;
            }
          }
  
          return new Tuple<string, int>(Encoding.UTF8.GetString(buffer_out), primary_index);
        }

        public static string Decode(string s, int primary_index)
        {
            if (primary_index >= s.Length)
            {
              return string.Empty;
            }
            
            if (string.IsNullOrEmpty(s))
            {
              return string.Empty;
            }
            
            var buffer_in = Encoding.UTF8.GetBytes(s);
            var buffer_decode = new byte[buffer_in.Length];
            var size = buffer_in.Length;
            var F = new byte[size];
            var buckets = new int[0x100];
            var indices = new int[size];
          
            for (var i = 0; i < 0x100; i++)
              buckets[i] = 0;
          
            for (var i = 0; i < size; i++)
              buckets[buffer_in[i]]++;
          
            for (int i = 0, k = 0; i < 0x100; i++)
            {
              for (var j = 0; j < buckets[i]; j++)
              {
                F[k++] = (byte)i;
              }
            }
          
            for (int i = 0, j = 0; i < 0x100; i++)
            {
              while (i > F[j] &amp;&amp; j < size - 1)
              {
                j++;
              }
              buckets[i] = j;
            }
          
            for (var i = 0; i < size; i++)
              indices[buckets[buffer_in[i]]++] = i;
          
            for (int i = 0, j = primary_index; i < size; i++)
            {              
              buffer_decode[i] = buffer_in[j];
              j = indices[j];
            }
            var listBufferDecode = buffer_decode.ToList();
            var firstByte = buffer_decode[0];
            listBufferDecode.RemoveAt(0);
            listBufferDecode.Add(firstByte);
            
            return Encoding.UTF8.GetString(listBufferDecode.ToArray());
        }
    }
    
    class BWTComparator : IComparer<int>
{
  private byte[] rotlexcmp_buf = null;
  private int rottexcmp_bufsize = 0;

  public BWTComparator(byte[] array, int size)
  {
    rotlexcmp_buf = array;
    rottexcmp_bufsize = size;
  }

  public int Compare(int li, int ri)
  {
    int ac = rottexcmp_bufsize;
    while (rotlexcmp_buf[li] == rotlexcmp_buf[ri])
    {
      if (++li == rottexcmp_bufsize)
        li = 0;
      if (++ri == rottexcmp_bufsize)
        ri = 0;
      if (--ac <= 0)
        return 0;
    }
    if (rotlexcmp_buf[li] > rotlexcmp_buf[ri])
      return 1;

    return -1;
  }
}

答案4:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
         if (string.IsNullOrEmpty(s))
                Environment.Exit(0);
             string x = s;
            List<string> result = new List<string>();
            string ResultEncode="";
            for (int i = 0; i < s.Length; i++)
            {
                x = x.Last() + x.Substring(0, x.Length - 1);
                result.Add(x);
            }
             result.Sort(StringComparer.Ordinal);
            foreach (string s1 in result)
            {
                ResultEncode += s1.Last();
            }
            return new Tuple<string, int>(ResultEncode,result.FindIndex(i => i==s ));
        }

        public static string Decode(string s, int i)
        {
             
            var x = s.ToCharArray().Select(c => c.ToString()).ToList();
            var y = s.ToCharArray().Select(c => c.ToString()).ToList();
            
            y.Sort(StringComparer.Ordinal);
            for (int j = 0; j < s.Length-1; j++)
            {
                
                int k = 0;
                foreach (string charac in x)
                {
                    y[k] = charac + y[k];
                    k++;
                }
                y.Sort(StringComparer.Ordinal);
            }

            return y[i];
        }
    }

答案5:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
    {
        public static string Decode(string s, int i)
        {
            var strings = s.OrderBy(x => x).Zip(s, (first, last) => new[] {last, first}).ToList();
            var values = strings.Select(x => new {Value = x, IndexFirst = strings.Take(strings.IndexOf(x)).Count(y => y[0] == x[0]), IndexLast = strings.Take(strings.IndexOf(x)).Count(y => y[1] == x[1])}).ToList();
            return s.Aggregate(new {Value = values.Count > i &amp;&amp; i >= 0 ? values[i] : null, Result = string.Empty}, (y, q) => new {Value = values.Single(x => x.Value[0] == y.Value.Value[1] &amp;&amp; x.IndexFirst == y.Value.IndexLast), Result = y.Result + y.Value.Value[1]}).Result;
        }

        public static Tuple<string, int> Encode(string s)
        {
            int rowIndex = 0;
            return Tuple.Create(new string(s.Select((val, i) => new string(s.Skip(s.Length - i).Concat(s.Take(s.Length - i)).ToArray())).OrderBy(x => x, StringComparer.Ordinal).Select((val, i) =>
            {
                if (val == s) rowIndex = i;
                return val.Last();
            }).ToArray()), rowIndex);
        }
    }

答案6:

using System;
using System.Collections.Generic;
using System.Linq;

public static class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
            if (string.IsNullOrEmpty(s))
                return Tuple.Create(s, 0);

            // 產生所有組合
            var output = Enumerable.Range(0, s.Length)
                .Select(index => $"{s.Substring(index, s.Length - index)}{s.Substring(0, index)}")
                .OrderAll(s.Length)
                .ToArray();

            // 取得最後一排文字
            var encode = output.Select(word => word.Last())
                .JoinToString();

            // 取得Index
            var number = output.Select((word, i) => new { Index = i, Word = word })
                .Where(item => item.Word == s)
                .First().Index;

            return Tuple.Create(encode, number);
        }

        public static string Decode(string s, int i)
        {
            // Happy coding

            if (string.IsNullOrEmpty(s) || i < 0)
                return "";

            var addWord = s.Select(c => new string(new char[] { c })).ToArray();
            var output = addWord.OrderBy(word => word.First())
                .AddItemAndOrder(addWord, s.Length)
                .ElementAt(i);

            return output;
        }


        
    }

    internal static class Ex
    {
        internal static IEnumerable<string> OrderAll(this IEnumerable<string> source, int count)
        {
            var tmp = source.OrderBy(word => word.First());

            foreach (var i in Enumerable.Range(0, count).Skip(1))
                tmp = tmp.ThenBy(word => word.Skip(i).First());

            return tmp;
        }

        internal static IEnumerable<string> AddItemAndOrder(this IEnumerable<string> source, string[] addWord, int count)
        {
            var orderList = source.OrderBy(word => word.First());

            for (var index = 1; index < count; index++)
                orderList = orderList.Select((word, _index) => addWord[_index] + word)
                    .OrderBy(word => word.First());

            return orderList;
        }

        public static string JoinToString(this IEnumerable<char> source)
        {
            return new string(source.ToArray());
        }
    }

答案7:

using System;
using System.Collections.Generic;
using System.Linq;

public class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
            if (s == "") return new Tuple<string, int>(s, 0);
            
            var matrix = new List<string>();
            for (int i = 0; i < s.Length; i++) matrix.Add(s.Substring(i) + s.Substring(0, i));
            matrix.Sort(StringComparer.Ordinal);

            int index = matrix.FindIndex(m => m == s);
            string last_column = matrix.Aggregate("", (lc, m) => lc + m[m.Length - 1]);
            return new Tuple<string, int>(last_column, index);
        }

        public static string Decode(string s, int i)
        {
            if (s == "") return s;

            var matrix = s.OrderBy(c => c).Select(c => c.ToString()).ToList();
            for (int k = 0; k < s.Length - 1; k++)
            {
                var temp = new List<string>();
                for (int j = 0; j < s.Length; j++) temp.Add(s[j] + matrix[j]);
                temp.Sort(StringComparer.Ordinal);
                matrix = temp;
            }

            return matrix[i];
        }
    }

答案8:

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

public class Kata
    {
        public static Tuple<string, int> Encode(string s)
        {
            int pos = 0;
            int idex = 0;
            StringBuilder sb = new StringBuilder(s.Length);
            foreach (var rot in AllRots(s).OrderBy(x => x, StringComparer.Ordinal))
            {
                sb.Append(rot[rot.Length - 1]);
                if (rot == s)
                    pos = idex;
                idex++;
            }
            return new Tuple<string,int> (sb.ToString(), pos);
        }

        private static IEnumerable<string> AllRots(string input)
        {
            for(int i=0; i<input.Length; i++)
            {
                yield return input.Substring(input.Length - i) + input.Substring(0, input.Length - i);
            }
        }


        public static string Decode(string s, int i)
        {
            string first = String.Join("", s.OrderBy(c => c));
            string last = s;
            var rots = Enumerable.Range(0, s.Length)
                .Select(x => last[x].ToString() + first[x].ToString())
                .OrderBy(st => st, StringComparer.Ordinal)
                .ToList();
            for (int x=2; x < s.Length; x++)
            {
                for(int j=0; j < s.Length; j++)
                {
                    rots[j] = last[j].ToString() + rots[j];
                }
                rots.Sort(StringComparer.Ordinal);
            }

            if(s == String.Empty)
              return String.Empty;
            else
              return rots[i];
        }
    }

答案9:

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

public class Kata
{
  public static Tuple<string, int> Encode(string s)
  {
    if (string.IsNullOrEmpty(s)) return new Tuple<string, int>(s, 0);
    
    string[] ss = new string[s.Length];
    ss[0] = s;
    for (int i = 1; i < s.Length; i++)
    {
      ss[i] = ss[i - 1].Substring(1) + ss[i - 1][0];
    }
    Array.Sort(ss, StringComparer.Ordinal);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < s.Length; i++)
    {
      sb.Append(ss[i].Last());
    }
    return new Tuple<string, int>(sb.ToString(), Array.IndexOf(ss, s));
  }

  public static string Decode(string s, int i)
  {
    if (string.IsNullOrEmpty(s)) return s;
  
    string t = new string(s.OrderBy(c => c).ToArray());
    
    char first = t[i];
    char last = s[i];
    
    List<Tuple<char, char>> list = new List<Tuple<char, char>>();
    for (int j = 0; j < s.Length; j++)
    {
      if (j == i) continue;
      list.Add(new Tuple<char, char>(s[j], t[j]));
    }

    foreach (string result in Find(first.ToString(), list))
    {
      if (result.Last() == last)
      {
        Tuple<string, int> encode = Encode(result);
        if (encode.Item1 == s &amp;&amp; encode.Item2 == i) return result;
      }
    }
    
    return "Boom!!!";
  }
  
  private static IEnumerable<string> Find(string s, List<Tuple<char, char>> list)
  {
    if (!list.Any()) yield return s;
    char last = s.Last();
    for (int i = 0; i < list.Count; i++)
    {
      Tuple<char, char> current = list[i];
      if (current.Item1 == last)
      {
        list.RemoveAt(i);
        foreach (string t in Find(s + current.Item2, list))
        {
          yield return t;
        }
        list.Insert(i, current);
      }
    }
  }
}

答案10:

using System;
using System.Collections.Generic;
using System.Linq;

    public class Kata
    {
        static void rotate(ref char[] ch)
        {
            int j;
            char c = ch[ch.Length - 1];
            for (j = ch.Length - 1; j > 0; j--)
            {
                ch[j] = ch[j - 1];
            }
            ch[j] = c;
        }

        static void OrderStrArray(ref string[] s)
        {
            bool cont = true;
            int i;
            string r;

            while (cont)
            {
                cont = false;
                i = 0;
                while (i < s.Length - 1)
                {
                    if (String.Compare(s[i], s[i + 1], StringComparison.Ordinal) > 0)
                    {
                        r = s[i];
                        s[i] = s[i + 1];
                        s[i + 1] = r;
                        cont = true;
                    }
                    i++;
                }
            }
        }

        public static Tuple<string, int> Encode(string s)
        {
            int i, index = 0;
            string[] matrix = new string[s.Length];
            char[] charS = s.ToCharArray();
            if(s.Length == 0)
            {
              return new Tuple<string, int>("", 0);
            }
            for (i = 0; i < s.Length; i++)
            {
                matrix[i] = new string(charS);
                rotate(ref charS);
            }
            OrderStrArray(ref matrix);
            for (i = 0; i < s.Length; i++)
            {
                charS[i] = matrix[i][s.Length - 1];
                if (matrix[i] == s)
                {
                    index = i;
                }
            }
            return new Tuple<string, int>(new string(charS), index);
        }

        public static string Decode(string s, int i)
        {
            int j, k;
            string[] matrix = new string[s.Length];
            
            if(s.Length == 0)
            {
              return "";
            }

            for (j = 0; j < s.Length; j++)
            {
                matrix[j] = "";
            }
            for (j = 0; j < s.Length; j++)
            {
                for (k = 0; k < s.Length; k++)
                {
                    matrix[k] = matrix[k].PadLeft(matrix[k].Length + 1, s[k]);
                }
                OrderStrArray(ref matrix);
            }
            return matrix[i];
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值