当前位置: 首页> 文旅> 酒店 > 百度网站收录提交入口在哪_搜索公司信息的软件_网站怎么推广效果好一点呢_推广关键词怎么设置

百度网站收录提交入口在哪_搜索公司信息的软件_网站怎么推广效果好一点呢_推广关键词怎么设置

时间:2025/7/13 23:29:36来源:https://blog.csdn.net/weixin_30777913/article/details/146959547 浏览次数:2次
百度网站收录提交入口在哪_搜索公司信息的软件_网站怎么推广效果好一点呢_推广关键词怎么设置

用C#实现搜索字符串中用’(‘和’)‘包裹的最外层的里面里面的字符串,将里面的记录按一个或多个空格、换行或tab,或者是它的在一起的组合作为分隔,分隔出多个字符串组,如果组中有字符串中同时包含’<‘和’>’,则在原始的字符串中该字符串的位置的前后插入字符`,最后返回修改过后的字符串,优化这个字符串处理算法,并给出所有的测试用例。

以下是C#代码,包括优化后的算法和测试用例:

using System;
using System.Collections.Generic;
using System.Text;public class StringProcessor
{public string ProcessString(string input){var outerPairs = FindOuterParenthesesPairs(input);List<int> insertPositions = new List<int>();foreach (var (start, end) in outerPairs){if (start > end)continue;string substr = input.Substring(start, end - start + 1);var groups = SplitIntoGroups(substr);foreach (var group in groups){int groupStartInSubstr = group.Start;int groupEndInSubstr = group.End;bool hasLT = false;bool hasGT = false;for (int i = groupStartInSubstr; i <= groupEndInSubstr; i++){char c = substr[i];if (c == '<') hasLT = true;else if (c == '>') hasGT = true;if (hasLT && hasGT) break;}if (hasLT && hasGT){int originalStart = start + groupStartInSubstr;int originalEnd = start + groupEndInSubstr;insertPositions.Add(originalStart);insertPositions.Add(originalEnd + 1);}}}insertPositions.Sort((a, b) => b.CompareTo(a));StringBuilder sb = new StringBuilder(input);foreach (int pos in insertPositions){sb.Insert(pos, '`');}return sb.ToString();}private List<(int Start, int End)> FindOuterParenthesesPairs(string input){List<(int, int)> pairs = new List<(int, int)>();int depth = 0;int currentStart = -1;for (int i = 0; i < input.Length; i++){char c = input[i];if (c == '('){depth++;if (depth == 1){currentStart = i + 1;}}else if (c == ')'){depth--;if (depth == 0 && currentStart != -1){int currentEnd = i - 1;if (currentStart <= currentEnd){pairs.Add((currentStart, currentEnd));}currentStart = -1;}}}return pairs;}private List<(int Start, int End)> SplitIntoGroups(string substr){List<(int, int)> groups = new List<(int, int)>();int currentGroupStart = -1;for (int i = 0; i < substr.Length; i++){if (IsWhitespace(substr[i])){if (currentGroupStart != -1){groups.Add((currentGroupStart, i - 1));currentGroupStart = -1;}}else{if (currentGroupStart == -1){currentGroupStart = i;}}}if (currentGroupStart != -1){groups.Add((currentGroupStart, substr.Length - 1));}return groups;}private bool IsWhitespace(char c){return c == ' ' || c == '\t' || c == '\n' || c == '\r';}
}public class Program
{public static void Main(){var processor = new StringProcessor();RunTestCases(processor);}private static void RunTestCases(StringProcessor processor){// 测试用例TestCase(processor, "", ""); // 空字符串TestCase(processor, "hello world", "hello world"); // 无括号TestCase(processor, "()", "()"); // 空括号TestCase(processor, "(a b c)", "(a b c)"); // 无<和>TestCase(processor, "(a <b> c)", "(a `<b>` c)"); // 单个<和>TestCase(processor, "(a <b) c)", "(a <b) c)"); // 不匹配的<和)TestCase(processor, "((a <b>))", "((a `<b>`))"); // 嵌套括号TestCase(processor, "(a\t<b>\n)", "(a\t`<b>`\n)"); // 混合空白符TestCase(processor, "( <a> <b> )", "( `<a>` `<b>` )"); // 多个组TestCase(processor, "start(middle<test> end)finish", "start(middle `<test>` end)finish"); // 混合内容TestCase(processor, "(a<b<c>>d)", "(a`<b<c>>`d)"); // 复杂嵌套<>}private static void TestCase(StringProcessor processor, string input, string expected){string result = processor.ProcessString(input);Console.WriteLine($"Input:    {input}");Console.WriteLine($"Expected: {expected}");Console.WriteLine($"Result:   {result}");Console.WriteLine($"Test {(result == expected ? "Passed" : "Failed")}\n");}
}

测试用例说明:

  1. 空字符串:输入为空,输出保持不变。
  2. 无括号:输入中没有括号,直接返回原字符串。
  3. 空括号:括号内无内容,不做处理。
  4. 无<和>的组:括号内的组不包含<和>,不插入反引号。
  5. 单个符合条件的组:组包含<和>,插入反引号。
  6. 不匹配的符号:组内只有<,不插入。
  7. 嵌套括号:仅处理最外层括号内的内容。
  8. 混合空白符:正确处理制表符和换行符。
  9. 多个组:每个符合条件的组独立处理。
  10. 混合内容:括号内外混合内容处理。
  11. 复杂嵌套符号:组内包含多个<和>符号。

组中全部字符串识别并包裹的实现。

using System;
using System.Collections.Generic;
using System.Text;public class StringProcessor
{public string ProcessString(string input){var outerPairs = FindOuterParenthesesPairs(input);List<int> insertPositions = new List<int>();foreach (var (start, end) in outerPairs){if (start > end)continue;string substr = input.Substring(start, end - start + 1);var groups = SplitIntoGroups(substr);foreach (var group in groups){int groupStartInSubstr = group.Start;int groupEndInSubstr = group.End;bool hasLT = false;bool hasGT = false;for (int i = groupStartInSubstr; i <= groupEndInSubstr; i++){char c = substr[i];if (c == '<') hasLT = true;else if (c == '>') hasGT = true;if (hasLT && hasGT) break;}if (hasLT && hasGT){int originalStart = start + groupStartInSubstr;int originalEnd = start + groupEndInSubstr;insertPositions.Add(originalStart);insertPositions.Add(originalEnd + 1);}}}insertPositions.Sort((a, b) => b.CompareTo(a));StringBuilder sb = new StringBuilder(input);foreach (int pos in insertPositions){sb.Insert(pos, '`');}return sb.ToString();}private List<(int Start, int End)> FindOuterParenthesesPairs(string input){List<(int, int)> pairs = new List<(int, int)>();int depth = 0;int currentStart = -1;for (int i = 0; i < input.Length; i++){char c = input[i];if (c == '('){depth++;if (depth == 1){currentStart = i + 1;}}else if (c == ')'){depth--;if (depth == 0 && currentStart != -1){int currentEnd = i - 1;if (currentStart <= currentEnd){pairs.Add((currentStart, currentEnd));}currentStart = -1;}}}return pairs;}private List<(int Start, int End)> SplitIntoGroups(string substr){List<(int, int)> groups = new List<(int, int)>();int currentGroupStart = -1;for (int i = 0; i < substr.Length; i++){if (IsWhitespace(substr[i])){if (currentGroupStart != -1){groups.Add((currentGroupStart, i - 1));currentGroupStart = -1;}}else{if (currentGroupStart == -1){currentGroupStart = i;}}}if (currentGroupStart != -1){groups.Add((currentGroupStart, substr.Length - 1));}return groups;}private bool IsWhitespace(char c){return c == ' ' || c == '\t' || c == '\n' || c == '\r';}
}public class Program
{public static void Main(){var processor = new StringProcessor();RunTestCases(processor);}private static void RunTestCases(StringProcessor processor){TestCase(processor, "", "");TestCase(processor, "hello world", "hello world");TestCase(processor, "()", "()");TestCase(processor, "(a b c)", "(a b c)");TestCase(processor, "(a <b> c)", "(a `<b>` c)");TestCase(processor, "(a <b) c)", "(a <b) c)");TestCase(processor, "((a <b>))", "((a `<b>`))");TestCase(processor, "(a\t<b>\n)", "(a\t`<b>`\n)");TestCase(processor, "( <a> <b> )", "( `<a>` `<b>` )");TestCase(processor, "start(middle<test> end)finish", "start(middle `<test>` end)finish");TestCase(processor, "(a<b<c>>d)", "(a`<b<c>>`d)");}private static void TestCase(StringProcessor processor, string input, string expected){string result = processor.ProcessString(input);Console.WriteLine($"Input:    {input}");Console.WriteLine($"Expected: {expected}");Console.WriteLine($"Result:   {result}");Console.WriteLine($"Test {(result == expected ? "Passed" : "Failed")}\n");}
}

测试用例说明:

  1. 空字符串:无处理。
  2. 无括号:直接返回。
  3. 空括号:内容为空,无处理。
  4. 无特殊符号组:不插入反引号。
  5. 单个有效组:正确插入反引号。
  6. 符号不匹配:不插入。
  7. 嵌套括号:仅处理最外层。
  8. 混合空白符:正确分割组。
  9. 多个有效组:每个组独立处理。
  10. 混合内容:仅处理括号内部分。
  11. 复杂嵌套符号:整个组被包裹(可能预期与实际不符,需确认需求)。
关键字:百度网站收录提交入口在哪_搜索公司信息的软件_网站怎么推广效果好一点呢_推广关键词怎么设置

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: