C#实现HiveQL建表语句中特殊数据类型的包裹
用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");}
}
测试用例说明:
- 空字符串:输入为空,输出保持不变。
- 无括号:输入中没有括号,直接返回原字符串。
- 空括号:括号内无内容,不做处理。
- 无<和>的组:括号内的组不包含<和>,不插入反引号。
- 单个符合条件的组:组包含<和>,插入反引号。
- 不匹配的符号:组内只有<,不插入。
- 嵌套括号:仅处理最外层括号内的内容。
- 混合空白符:正确处理制表符和换行符。
- 多个组:每个符合条件的组独立处理。
- 混合内容:括号内外混合内容处理。
- 复杂嵌套符号:组内包含多个<和>符号。
组中全部字符串识别并包裹的实现。
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");}
}
测试用例说明:
- 空字符串:无处理。
- 无括号:直接返回。
- 空括号:内容为空,无处理。
- 无特殊符号组:不插入反引号。
- 单个有效组:正确插入反引号。
- 符号不匹配:不插入。
- 嵌套括号:仅处理最外层。
- 混合空白符:正确分割组。
- 多个有效组:每个组独立处理。
- 混合内容:仅处理括号内部分。
- 复杂嵌套符号:整个组被包裹(可能预期与实际不符,需确认需求)。
相关文章:
C#实现HiveQL建表语句中特殊数据类型的包裹
用C#实现搜索字符串中用’(‘和’)‘包裹的最外层的里面里面的字符串,将里面的记录按一个或多个空格、换行或tab,或者是它的在一起的组合作为分隔,分隔出多个字符串组,如果组中有字符串中同时包含’<‘和’>’,则…...
【idea】实用插件
SonarLint SonarLint:代码质量扫描工具 使用 SonarLint 可以帮助我们发现代码的问题,并且还提供了相应的解决方案. 对于每一个问题,SonarLint 都给出了示例,还有相应的解决方案,教我们怎么修改,极大的方便了我们的开发…...
关于mysql 数据库中的 慢SQL 的详细分析,包括定义、原因、解决方法及表格总结
以下是关于 慢SQL 的详细分析,包括定义、原因、解决方法及表格总结: 1. 什么是慢SQL? 定义: 慢SQL 是指执行时间超过预设阈值(如 2 秒)的 SQL 语句,通常会导致数据库响应延迟、资源占用过高&am…...
uniapp选择文件使用formData格式提交数据
1. Vue实现 在vue项目中,我们有个文件,和一些其他字段数据需要提交的时候,我们都是使用axios 设置请求头中的Content-Type: multipart/form-data,然后new FormData的方式来进行提交。方式如下: const sendRequest = () => {const formData = new FormData()formData…...
蓝牙数字音频和模拟音频优劣势对比?
蓝牙模块中我们常说的模拟音频和数字音频,是指两种不同的信号处理技术,它们都可以实现声音的录制、存储、编辑、压缩或播放,但也有一些区别和特点。本文将为您深入解析蓝牙数字音频和模拟音频的一些常见区别。 数字音频: 蓝牙数…...
WiFi(无线局域网)技术的多种工作模式
WiFi(无线局域网)技术支持多种工作模式,以满足不同的网络需求和应用场景。以下是主要的WiFi工作模式及其详细说明: 1. 基础设施模式(Infrastructure Mode) [无线接入点 (AP)]/ | \ [客户端…...
基于OpenCV的指纹验证:从原理到实战的深度解析
指纹识别的技术革命与OpenCV的轻量级方案 在生物特征识别领域,指纹识别始终以独特性和稳定性占据核心地位。随着OpenCV等开源视觉库的普及,这项看似"高大上"的技术正逐步走向民用化开发。本文将突破传统算法框架,提出一套基于OpenC…...
VMware+Ubuntu+VScode+ROS一站式教学+常见问题解决
目录 一.VMware的安装 二.Ubuntu下载 1.前言 2.Ubuntu版本选择 三.VMware中Ubuntu的安装 四.Ubuntu系统基本设置 1.中文更改 2.中文输入法更改 3. 辅助工具 vmware tools 五.VScode的安装ros基本插件 1.安装 2.ros辅助插件下载 六.ROS安装 1.安装ros 2.配置ROS…...
音视频(一)ZLMediaKit搭建部署
前言 一个基于C11的高性能运营级流媒体服务框架 全协议支持H264/H265/AAC/G711/OPUS/MP3,部分支持VP8/VP9/AV1/JPEG/MP3/H266/ADPCM/SVAC/G722/G723/G729 1:环境 ubuntu22.* ZLMediaKit downlaod:https://github.com/ZLMediaKit/ZLMediaKit or https://g…...
leetcode25.k个一组翻转链表
思路源自 【力扣hot100】【LeetCode 25】k个一组翻转链表|虚拟节点的应用 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(in…...
配置 UOS/deepin 系统远程桌面,实现多台电脑协同办公
由于开发工作的需要,我的办公桌上目前有多台电脑。一台是 i7 配置的电脑,运行 UOS V20 系统,作为主力办公电脑,负责处理企业微信、OA 等任务,并偶尔进行代码编译和验证软件在 UOS V20 系统下的兼容性;另一台…...
配置Next.js环境 使用vscode
配置 Next.js 的开发环境其实非常简单,下面是一个从零开始的完整步骤,适用于 Windows、macOS 和 Linux: ✅ 一、准备工作 确保你已经安装了以下软件: 1. Node.js(推荐 LTS 版本) 官网:https:/…...
Vite相关知识点
一、自动导入vue vue-router pinia 1、安装unplugin-auto-import npm install unplugin-auto-import -D 2、引入 import AutoImport from unplugin-auto-import/vite; 3、配置vite.config.ts plugins: [ vue(), vueDevTools(), AutoImport({ include: [ /…...
RCE复现
1.过滤flag <?php error_reporting(0); if(isset($_GET[c])){$c $_GET[c];if(!preg_match("/flag/i", $c)){eval($c);}}else{highlight_file(__FILE__);代码审计过滤了"flag"关键词,但限制较弱,容易绕过 ?csystem("ls&…...
电子电气架构 --- 域控制器和EE架构关系
我是穿拖鞋的汉子,魔都中坚持长期主义的汽车电子工程师。 老规矩,分享一段喜欢的文字,避免自己成为高知识低文化的工程师: 周末洗了一个澡,换了一身衣服,出了门却不知道去哪儿,不知道去找谁,漫无目的走着,大概这就是成年人最深的孤独吧! 旧人不知我近况,新人不知我过…...
多输入多输出 | Matlab实现CPO-LSTM冠豪猪算法优化长短期记忆神经网络多输入多输出预测
多输入多输出 | Matlab实现CPO-LSTM冠豪猪算法优化长短期记忆神经网络多输入多输出预测 目录 多输入多输出 | Matlab实现CPO-LSTM冠豪猪算法优化长短期记忆神经网络多输入多输出预测预测效果基本介绍程序设计参考资料 预测效果 基本介绍 Matlab实现CPO-LSTM冠豪猪算法优化长短期…...
使用PyTorch实现LeNet-5并在Fashion-MNIST数据集上训练
本文将展示如何使用PyTorch实现经典的LeNet-5卷积神经网络,并在Fashion-MNIST数据集上进行训练和评估。代码包含完整的网络定义、数据加载、训练流程及结果可视化。 1. 导入依赖库 import torch from torch import nn from d2l import torch as d2l 2. 定义LeNet…...
19_20 js es6
目录 ES6 一、let 和 const关键字 1.1 var 和 let const的区别? 1.2 let 和const的区别 1.3 关于块级作用域 二、箭头函数 2.1箭头函数的特点 2.2 箭头函数的特殊性 this的问题 arguments参数集合 2.3函数传递参数时的默认值 2.4 箭头函数使用的场景有哪…...
自动化释放linux服务器内存脚本
脚本说明 使用Linux的Cron定时任务结合Shell脚本来实现自动化的内存释放。 脚本用到sync系统命令 sync的作用:sync 是一个 Linux 系统命令,用于将文件系统缓存中的数据强制写入磁盘。 在你执行reboot、poweroff、shutdown命令时,系统会默认执…...
【强化学习】近端策略优化算法(PPO)的理解
本篇博客参考自上海大学刘树林老师的课程。B站课程链接:https://www.bilibili.com/video/BV17t4geUEvQ/?spm_id_from333.337.search-card.all.click&vd_source74af336a587568c23a499122c8ffbbee 文章目录 传统策略梯度训练面临的问题其他方法的改进TRPO算法的贡…...
Java基础 3.30
1.结合练习 /*随机生成10个整数(1-100的范围)保存到数组,并倒序打印以及求平均值、求最大值和最大值的下标,并查找里面是否有8 */ public class ArrayHomework02 {public static void main(String[] args) {int arr[] new int[10];for (int i 0; i &l…...
5.好事多磨 -- TCP网络连接Ⅱ
前言 第4章节通过回声服务示例讲解了TCP服务器端/客户端的实现方法。但这仅是从编程角度的学习,我们尚未详细讨论TCP的工作原理。因此,将详细讲解TCP中必要的理论知识,还将给出第4章节客户端问题的解决方案。 一、回声客户端完美实现 第4章…...
【零基础入门unity游戏开发——2D篇】SpriteMask精灵遮罩组件
考虑到每个人基础可能不一样,且并不是所有人都有同时做2D、3D开发的需求,所以我把 【零基础入门unity游戏开发】 分为成了C#篇、unity通用篇、unity3D篇、unity2D篇。 【C#篇】:主要讲解C#的基础语法,包括变量、数据类型、运算符、…...
Java 枚举类 Key-Value 映射的几种实现方式及最佳实践
Java 枚举类 Key-Value 映射的几种实现方式及最佳实践 前言 在 Java 开发中,枚举(Enum)是一种特殊的类,它能够定义一组固定的常量。在实际应用中,我们经常需要为枚举常量添加额外的属性,并实现 key-value 的映射关系。本文将详细…...
JVM 每个区域分别存储什么数据?
JVM(Java Virtual Machine)的运行时数据区(Runtime Data Areas)被划分为几个不同的区域,每个区域都有其特定的用途和存储的数据类型。以下是 JVM 各个区域存储数据的详细说明: 1. 程序计数器 (Program Cou…...
chromem-go + ollama + bge-m3 进行文档向量嵌入和查询
Ollama 安装 https://ollama.com/download Ollama 运行嵌入模型 bge-m3:latest ollama run bge-m3:latestchromem-go 文档嵌入和查询 package mainimport ("context""fmt""runtime""github.com/philippgille/chromem-go" )func ma…...
PyTorch中卷积层torch.nn.Conv2d
在 PyTorch 中,卷积层主要由 torch.nn.Conv1d、torch.nn.Conv2d 和 torch.nn.Conv3d 实现,分别对应一维、二维和三维卷积操作。以下是详细说明: 1. 二维卷积 (Conv2d) - 最常用 import torch.nn as nn# 基本参数 conv nn.Conv2d(in_channe…...
GO语言学习(16)Gin后端框架
目录 ☀️前言 1.什么是前端?什么是后端?🌀 2.Gin框架介绍 🌷 3.Gin框架的基本使用 -Hello,World例子🌷 🌿入门示例 - Hello,World 💻补充(一些常用的网…...
RAG 在 AI 助手、法律分析、医学 NLP 领域的实战案例
RAG(Retrieval-Augmented Generation,检索增强生成)是一种结合信息检索和生成模型的技术,广泛应用于 AI 助手、法律分析、医学 NLP 等领域。 以下是具体的实战案例和技术实现。 1. AI 助手中的 RAG 应用 案例 1:企业…...
大模型-提示词(Prompt)技巧
1、什么是提示词? 提示词(Prompt)是用户发送给大语言模型的问题、指令或请求,用来明确地告诉模型用户想要解决的问题或完成的任务,是大语言模型理解用户需求并据此生成相关、准确回答或内容的基础。对于大语言模型来说…...
