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;
}
}
}