判断一段字符串是否合法算法求解?
判断一段字符串是否合法!
如题:
字符串中必须符合以下规则:
1.Y-YYYY 占1-4位
GG 占2 位
CC 占2 位
SS-SSS 占2-3位
N-NN...N 占1-11位
R-RR...R 占1-11位
2.字符串长度不能大于或小于11位
3.不允许用正则表达式
例如: YYYYGGCCSSS 这是合法
ASSSGGCCNNN 这是不合法,因为A不属于此规则集合范围中
GYYYYCCNNNN 这是不合法,因为G规定站2位
求如何写此函数
[解决办法]
1.YYYYGGCCSSS是不合法的,因为没有n和r
2.代码如下
using System;
class App
{
public static void Main()
{
Console.WriteLine(Check( "yggccssnnnr "));
Console.Read();
}
private static bool Check(string s)
{
if (s.Length != 11)
return false;
else
{
s = s.ToLower();
int[] a ={ 0, 0, 0, 0, 0, 0 };
foreach (char c in s)
{
switch (c)
{
case 'y ':
a[0] = a[0] + 1;
break;
case 'g ':
a[1] = a[1] + 1;
break;
case 'c ':
a[2] = a[2] + 1;
break;
case 's ':
a[3] = a[3] + 1;
break;
case 'n ':
a[4] = a[4] + 1;
break;
case 'r ':
a[5] = a[5] + 1;
break;
default:
return false;
}
}
if (a[0] > = 1 && a[0] <= 4 && a[1] == 2 && a[2] == 2 & a[3] > = 2 && a[3] <= 3 && a[4] > = 1 && a[4] <= 11 && a[5] > = 1 && a[5] <= 11)
return true;
else
return false;
}
}
}
[解决办法]
public bool IsValidString(string str)
{
if (str.Length!=11) return false;
int[] arr = new int[6];
int n;
foreach(char c in str)
{
switch(c)
{
case 'Y ':
if (n!=0 && arr[0]!=0) return false;//非连续,退出
arr[0]++;
n = 0;
break;
case 'G ':
if (n!=1 && arr[1]!=0) return false;//非连续,退出
arr[1]++;
n = 1;
break;
case 'C ':
if (n!=2 && arr[2]!=0) return false;//非连续,退出
arr[2]++;
n = 2;
break;
case 'S ':
if (n!=3 && arr[3]!=0) return false;//非连续,退出
arr[3]++;
n = 3;
break;
case 'N ':
if (n!=4 && arr[4]!=0) return false;//非连续,退出
arr[4]++;
n = 4;
break;
case 'R ':
if (n!=5 && arr[5]!=0) return false;//非连续,退出
arr[5]++;
n = 5;
break;
default://有其它字符,退出
return false;
}
}
if (arr[0] > 4) return false;
if (arr[1] != 2) return false;
if (arr[2] != 2) return false;
if (arr[3] <2 || arr[3]> 3) return false;
return true;
}
[解决办法]
bool checknum(string input,int[][] keys,int minlength,int maxLength)
{
if(input.Length <minLength && input.Length> maxLength)
{
return false;
}
char[] chars=input.ToCharArray();
int i =0;
char current=chars[0];
int charcount=0;
int count =chars.Length;
for(i=0;i <count;i++)
{
if(current!=chars[i] && i!=0)
{
int keyindex=Array.IndexOf(keys[0],(int)current);
if(keyindex <0)
{
return false;
}
else
{
if(charcount <keys[1][keyindex] || charcount> keys[2][keyindex])
{
return false;
}
}
current=chars[i];
charcount=1;
}
else
{
charcount+=1;
}
}
return true;
}
private void button1_Click(object sender, System.EventArgs e)
{
string s= "YYYYGGCCSSS ";
int[][] keys= new int[3][];
keys[0]=new int [] { 'Y ', 'G ', 'C ', 'N ', 'S ', 'R '};
keys[1] = new int[]{1,2,2,3,1,1};
keys[2]=new int[]{4,2,2,3,11,11};
MessageBox.Show(this.checknum(s,keys,11,11).ToString());
}
[解决办法]
/// <summary>
/// Specified Alpha List
/// </summary>
private string[] checkList;
/// <summary>
/// Specified Alpha Request
/// </summary>
Dictionary <string, int[]> request;
public Form1()
{
InitializeComponent();
checkList = new string[] { "Y ", "G ", "C ", "S ", "N ", "R " };
request = new Dictionary <string, int[]> ();
request[ "Y "] = new int[] { 1, 4 };
request[ "G "] = new int[] { 2, 2 };
request[ "C "] = new int[] { 2, 2 };
request[ "S "] = new int[] { 2, 3 };
request[ "N "] = new int[] { 1, 11 };
request[ "R "] = new int[] { 1, 11 };
}
/// <summary>
/// Test Button Click Event Handler
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
private void button1_Click(object sender, EventArgs e)
{
bool result = CheckLine(textBox1.Text);//Get the test string from a TextBox
if (result) MessageBox.Show( "Good Message... ", "Info ", MessageBoxButtons.OK, MessageBoxIcon.Information);
else MessageBox.Show( "Invalid Message... ", "Info ",MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
/// <summary>
/// The Validation Checking Entry Point
/// </summary>
/// <param name= "line "> Input Line </param>
/// <returns> </returns>
private bool CheckLine(string line)
{
bool result = false;
string tmpLine = line;
int index = 0;
if (line.Length == 11)
{
foreach (string alpha in checkList)
{
//check if there any alpha not in the checkList
tmpLine = tmpLine.Replace(alpha, " ");
index++;
if (index == checkList.Length && tmpLine != " ") return false;
if (line.Contains(alpha))
{
MethodInfo vMethodInfo = this.GetType().GetMethod( "ValidationCheck ");
if (vMethodInfo != null)
{
result = (bool)vMethodInfo.Invoke(this, new object[] { line, alpha });
if (!result) break;
}
}
}
}
return result;
}
/// <summary>
/// The Input Line Validation Checking
/// </summary>
/// <param name= "line "> Input Line </param>
/// <param name= "alpha "> Alpha in Check List </param>
/// <returns> </returns>
public bool ValidationCheck(string line, string alpha)
{
bool ret = false;
int start = line.IndexOf(alpha);
int count = 0;
char[] chars = line.ToCharArray();
for (int i = start; i < chars.Length; i++)
{
if (chars[i] == char.Parse(alpha))
{
count++;
if (count > (request[alpha][0] - 1) && count < (request[alpha][1] + 1)) ret = true;
}
else
{
if (count > (request[alpha][0] - 1) && count < (request[alpha][1] + 1)) ret = true;
count = 0;
}
}
return ret;
}
[解决办法]
啊, 晕了, 居然还用了反射, 因为之前有多个相似的method去做ValidationCheck, 就用了MethodInfo来反射调用, 之后发现把它们统一就行了, 但我却还在用MethodInfo, 虽然无伤大雅, 但有点牛刀了, 把if(line.Contains(alpha))里面的code改成下面的:
if (line.Contains(alpha))
{
result = ValidationCheck(line, alpha);
if (!result) break;
}
[解决办法]
private bool VerifyStr(string s)
{
char [] k = { 'Y ', 'G ', 'C ', 'S ', 'N ', 'R '};
int [] upper = {4,2,2,3,11,11};
int [] lower = {1,2,2,2,1,1};
if (s.Length != 11)
{
return false;
}
char prev = s[0];
int len = 1;
for (int i = 1; i < s.Length; ++i)
{
if (s[i] == prev)
{
len++;
}
if ((s[i] != prev) || (i == s.Length - 1))
{
bool isAllowed = false;
for (int j = 0; j < k.Length; ++j)
{
if (k[j] == prev)
{
isAllowed = true;
if (len < lower[j] || len > upper[j])
return false;
break;
}
}
prev = s[i];
len = 1;
if (!isAllowed) return false;
}
}
return true;
}