当前位置: 首页 > news >正文

网站建设包含哪些建设阶段兰州展柜公司网站建设

网站建设包含哪些建设阶段,兰州展柜公司网站建设,深圳网站建设伪静态 报价 jsp 语言,建网站的费用包括题目链接 722. 删除注释 标签 字符串 步骤 Step1. 先将source合并为一个字符串进行处理,中间补上’\n’,方便后续确定注释开始、结束位置。 string combined; for (auto str : source) {combined str "\n"; }Step2. 定义数组 toDel&am…

题目链接

722. 删除注释

标签

字符串

步骤

Step1. 先将source合并为一个字符串进行处理,中间补上’\n’,方便后续确定注释开始、结束位置。

string combined;
for (auto str : source) {combined += str + "\n";
}

Step2. 定义数组 toDel,记录每一个注释开始、结束的位置;进行状态转移。对于 /**/ 类型的注释的结束符 */,如果匹配失败,则需要回退一位。

状态转移表示如下:

s0: if /: goto s1 # match /, change to s1else: goto s0
s1: if /: cut down this line, goto s0 # match //elif *: goto s2 # match /*else: goto s0 # match failed, return to s1
s2: if *: goto s3 # match * in comment; s2 means that the current state is InCommentelse: goto s2
s3: if /: end this comment, goto s0 # match */else: goto s3  # match failed, return to s2

具体的代码部分如下:

for (int i = 0; i < len; i++) {char ch = combined[i];switch (state) {case 0:if (ch == '/') {state = 1;} else {state = 0;}break;case 1:if (ch == '/') { // 出现//,删去此行之后的内容// 找到从当前下标开始的第一个\n,下一次遍历从其之后开始int end = combined.find("\n", i);cmtBegin = i - 1;cmtEnd = end - 1;toDel.push_back({cmtBegin, cmtEnd});state = 0;i = end;} else if (ch == '*') { // 出现/*state = 2;cmtBegin = i - 1;} else {state = 0;}break;case 2:if (ch == '*') {state = 3;} else {state = 2;}break;case 3:if (ch == '/') { // cmtEndstate = 0;cmtEnd = i;toDel.push_back({cmtBegin, cmtEnd});} else { // 匹配*/失败,回退一位state = 2;i--;}break;} 
}

Step3. 遍历 toDel,得到不含注释的中间结果 tmpAns

// 遍历toDel,得到删除注释的中间结果
string tmpAns;
int last = 0;
for (auto p : toDel) {// 下标a和b-1之间的长度:b-atmpAns += combined.substr(last, p.first - last);last = p.second + 1;
}
// 分为toDel.size()+1段的最后一段
tmpAns += combined.substr(last, combined.length() - last);

Step4. 根据 \n 来分割 tmpAns 即可。

vector<string> ans;
int pos = tmpAns.find("\n", 0);
while (pos != string::npos) {if (pos != 0) {ans.push_back(tmpAns.substr(0, pos));}tmpAns.erase(0, pos+1);pos = tmpAns.find("\n", 0);
}

实现代码(C++)

class Solution {
public:vector<string> removeComments(vector<string>& source) {int state = 0;// 合并为一个字符串string combined;for (auto str : source) {combined += str + "\n";}vector<pair<int,int>> toDel;int len = combined.length();int cmtBegin = -1, cmtEnd = -1;for (int i = 0; i < len; i++) {char ch = combined[i];switch (state) {case 0:if (ch == '/') {state = 1;} else {state = 0;}break;case 1:if (ch == '/') { // 出现//,删去此行之后的内容// 找到从当前下标开始的第一个\n,下一次遍历从其之后开始int end = combined.find("\n", i);cmtBegin = i - 1;cmtEnd = end - 1;toDel.push_back({cmtBegin, cmtEnd});state = 0;i = end;} else if (ch == '*') { // 出现/*state = 2;cmtBegin = i - 1;} else {state = 0;}break;case 2:if (ch == '*') {state = 3;} else {state = 2;}break;case 3:if (ch == '/') { // cmtEndstate = 0;cmtEnd = i;toDel.push_back({cmtBegin, cmtEnd});} else { // 匹配*/失败,回退一位state = 2;i--;}break;} }// 遍历toDel,得到删除注释的中间结果string tmpAns;int last = 0;for (auto p : toDel) {tmpAns += combined.substr(last, p.first - last);last = p.second + 1;}tmpAns += combined.substr(last, combined.length() - last);// 根据\n分割vector<string> ans;int pos = tmpAns.find("\n", 0);while (pos != string::npos) {if (pos != 0) {ans.push_back(tmpAns.substr(0, pos));}tmpAns.erase(0, pos+1);pos = tmpAns.find("\n", 0);}return ans;}
};
http://www.sczhlp.com/news/118241/

相关文章:

  • 清远住房和城乡建设部网站wordpress文章置顶排序
  • 企业网站前端模板9377将军
  • 网站和公众号的区别网页设计html代码成品图片
  • 查看网站的外链销售人员培训课程有哪些
  • 2015做哪个网站致富传统软件开发流程
  • 优质做网站哪家正规怎么制作免费建网站
  • 南京网站开发建设网站域名如何续费
  • 免费的网站入口在哪慈溪seo
  • ODT/珂朵莉树 入门
  • 青岛网站制作seo登录自治区建设厅的网站查询
  • 贵州住房和城乡建设厅官方网站建设银行的官方网站电脑版
  • 太原集团网站建设2023中国互联网公司排行榜
  • 网站开发 网站建设深圳网站建设犀牛云
  • 东台市住房和城乡建设局闷网站昆明网站空间
  • 海洋cms做电影网站好做吗搜索引擎网站分析
  • 网站开发微信支付功能网站建设找祥赢
  • 优质的成都网站建设推设计网站会员
  • 又一个 wordpress 站点广州建站快车
  • dw网站的站点建设微信小程序开发成本
  • 国内做视频的网站有哪些wordpress 浏览量 点击
  • 婚恋网站建设教程网站制作 代码
  • 旅游网站模块报价it软件外包
  • 网站logo大全网站建设指的是什么
  • 手机网站自动跳转怎么解决上海百度优化
  • 郑州网站排名公司网站顶部轮播怎么做的
  • 开发网站的硬件成本wordpress邮箱用不了
  • 贵州省建设厅城乡建设网站优秀网站案例
  • 天津市武清区网站建设四川专业网站建设
  • 百度网站免费优化软件下载参与网站建设的人员
  • 什么网站做美式软装设计一般网络推广应该怎么做