字符算法 包括字符旋转 包含等问题_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 字符算法 包括字符旋转 包含等问题

字符算法 包括字符旋转 包含等问题

 2013/8/25 17:55:09  迟钝的小D  博客园  我要评论(0)
  • 摘要:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Collections;namespaceConsoleApplication2{classStringSuan{//字符串是否包含问题A中的字符是否在B中出现publicstaticvoidContains(){stringstr1="ggwahrah";stringstr2="gwha"
  • 标签:问题 算法

using System;
using System.Collections.Generic;

using System.Text;

using System.Collections;
namespace ConsoleApplication2
{
    class StringSuan
    {
        //字符串是否包含问题 A中的字符是否在B中出现
        public static void Contains()
        {
            string str1 = "ggwahrah";
            string str2 = "gwha";

            // 开辟一个辅助数组并清零 
            int[] hash = new int[100];

            // num为辅助数组中元素个数 
            int num = 0;

            // 扫描短字符串 
            for (int j = 0; j < str2.Length; j++)
            {
                // 将字符转换成对应辅助数组中的索引 
                int index = str2[j] - 'A';

                // 如果辅助数组中该索引对应元素为0,则置1,且num++; 
                if (hash[index] == 0)
                {
                    hash[index] = 1;
                    num++;
                }
            }

            // 扫描长字符串 
            for (int k = 0; k < str1.Length; k++)
            {
                int index = str1[k] - 'A';

                // 如果辅助数组中该索引对应元素为1,则num--;为零的话,不作处理(不写语句)。 
                if (hash[index] == 1)
                {
                    hash[index] = 0;
                    num--;
                    if (num == 0)    //m==0,即退出循环。 
                        break;
                }
            }

            // num为0说明长字符串包含短字符串内所有字符 
            if (num == 0)
                Console.WriteLine("TRUE");
            else
                Console.WriteLine("FALSE");

        }

        ////////
        // 字符串左旋转
        public static void Reverse(StringBuilder sb, int index, int n)
        {

            for (int i = 0; i < n / 2; i++)
            {
                char temp = sb[index + i];
                sb[index + i] = sb[index + n - i - 1];
                sb[index + n - i - 1] = temp;
            }

        }
        public static void LeftRotate(StringBuilder sb, int index)
        {
            if (index > sb.Length)
            {
                Console.WriteLine("please insert smaller number");
                return;
            }
            Reverse(sb, 0, index);
            Reverse(sb, index, sb.Length - index);
            Reverse(sb, 0, sb.Length);
            Console.WriteLine(sb);
        }
        /////////

        //获得字符串中最大的连续数字get the longest number from a string
        public static void LongNumber(string n)
        {
            char[] str = n.ToCharArray();
            int max = 0, j = 0;
            int start = 0;
            bool set = false;
            int length = str.Length;
            int output = 0;
            for (int i = 0; i < length; i++)
            {
                if (char.IsNumber(str[i]))
                {
                    if (set == false)
                        start = i;
                    j++;
                    set = true;
                }

                else
                {
                    if (j > max)
                    {
                        output = start;
                        max = j;
                    }
                    set = false;
                    j = 0;
                }
            }
            if (j > max)
            {
                output = start;
                max = j;
            }


            for (int i = 0; i < max; i++)

                Console.Write(str[output + i]);
        }

        // 字符反转
        static string Reverse1(string original)
        {
            char[] arr = original.ToCharArray();
            Array.Reverse(arr);
            return new string(arr);

        }
        static void ver(char[] str)
        {
            char temp;
            int len;
            len = str.Length;
            for (int i = 0; i < len / 2; i++)
            {
                temp = str[i];
                str[i] = str[len - i - 1];
                str[len - i - 1] = temp;

            }
            Console.WriteLine(str);
        }

        //字符串中第一个只出现一次的字符“abcdabc”  output:d
        public static char? Print(string str)
        {
            if (str.Length == 0 || str == null)
                return null;

            bool[] marks = new bool[str.Length];

            for (int i = 0; i < str.Length; i++)
            {
                if (marks[i] != true)
                {
                    marks[i] = true;

                    int count = 0;

                    for (int j = i; j < str.Length; j++)
                    {
                        if (str[j] == str[i])
                        {
                            count++;
                            marks[j] = true;
                        }
                    }

                    if (count == 1)
                    {
                        return str[i];
                    }
                }
            }

            return null;
        } 
    }
}

上一篇: 2013年上半年我读过的那些书 下一篇: 没有下一篇了!
发表评论
用户名: 匿名