C# 基于特定字母从字符串中获取字母链

C# 基于特定字母从字符串中获取字母链,c#,notation,chain,letters,polish,C#,Notation,Chain,Letters,Polish,我有一个字符串,其中包含表示平面图(VLSI布局)的波兰符号,它包含类似“1234VHV56HV”的内容。 (仅供参考,这意味着:垂直分离3和4,然后水平分离结果;水平分离2,然后垂直分离结果;垂直分离1;水平分离5和6,然后垂直分离前两个结果。) 假设字符串变量名为:polishNotation。包含的字母仅为“V”表示垂直,或“H”表示水平 我试图应用一种称为“模拟退火”的算法来更改波兰语符号,因此我想随机选择一个索引(当然小于polishNotation.Length),如果该索引指向一个

我有一个字符串,其中包含表示平面图(VLSI布局)的波兰符号,它包含类似“1234VHV56HV”的内容。 (仅供参考,这意味着:垂直分离3和4,然后水平分离结果;水平分离2,然后垂直分离结果;垂直分离1;水平分离5和6,然后垂直分离前两个结果。)

假设字符串变量名为:polishNotation。包含的字母仅为“V”表示垂直,或“H”表示水平

我试图应用一种称为“模拟退火”的算法来更改波兰语符号,因此我想随机选择一个索引(当然小于polishNotation.Length),如果该索引指向一个字母('V'或'H'),我想得到包含它的字母链,然后将每个“V”改为“H”,并将每个“H”改为“V”。。。换言之:补充链

  • 例如:假设polishNotation=“1234VHV56HV”且随机指数=5,则结果为“H”。。。我想检索“VHV”,并将其补充为:“1234HVH56HV”
  • 另一个例子:假设polishNotation=“1234VHV56HV”和随机指数=9,那么结果是“H”。。。我想检索“HV”并将其补充为:“1234VHV56VH”
  • 另一个例子:假设polishNotation=“1234VHV56HV”和随机指数=6,那么结果是“V”。。。我想检索“VHV”,并将其补充为:“1234HVH56HV”

我希望我说清楚了。。。有什么建议吗?我正在使用C#.net

你可以试试这样的东西。我打赌有一种方法可以用正则表达式来实现这一点,但我一点也不知道

    string Complement(string floorPlan)
    {
        int index = rand.Next(floorPlan.Length); //get a random integer within array bounds

        if (floorPlan[index] != 'H' || floorPlan[index] != 'V') // if we didn't grab a letter, return
            return floorPlan;

        int start = index; //we'll need to find the start of the 'letter group'

        for (int i = index; i >= 0; i--) // work backwards through the string
            if (floorPlan[i] == 'H' || floorPlan[i] == 'V') // updating if we find another letter
                start = i;
            else // break when we don't
                break;            

        StringBuilder sb = new StringBuilder(floorPlan); // use a string builder for ease of char replacement

        for (int i = start; i < floorPlan.Length; i++) // using the start index, interate through
            if (floorPlan[i] == 'V') // and replace accordingly
                sb[i] = 'H';
            else if (floorPlan[i] == 'H')
                sb[i] = 'V';
            else // breaking when we encounter a number
                break;

        return sb.ToString();
    }
字符串补码(字符串布局图)
{
int index=rand.Next(floorPlan.Length);//获取数组边界内的随机整数
if(floorPlan[index]!='H'| | floorPlan[index]!='V')//如果我们没有抓到一封信,请返回
返回平面图;
int start=index;//我们需要找到“字母组”的开头
for(int i=index;i>=0;i--)//向后遍历字符串
if(floorPlan[i]='H'| | floorPlan[i]='V')//如果我们发现另一个字母,则更新
开始=i;
否则,当我们不这样做的时候,我们就会崩溃
打破
StringBuilder sb=new StringBuilder(floorPlan);//使用字符串生成器以便于替换字符
对于(int i=start;i
如果您想用C语言回答问题,您可能需要添加C作为标记