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