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

通过栈实现字符串中查找是否有指定字符串的存在

题目示例:
在这里插入图片描述

分析

由与没有给出字符串的长度,所以只能通过getline一次性处理,而在输入后恰好能倒序处理字符串,以标点符号为分界点,将数字当成字符放到栈里,遇到下一个标点符号时执行查找操作,并清空栈。

题不难,直接上代码实现吧:


//程序没考虑空格。请用这行没空格的样例 
//样例: matrix=[[1,3,5,7],[10,11,16,20],[23,30,34,60]],target=3
//matrix=[[1,3,5,7],[10,11,16,20],[23,30,34,60]],target=11#include <iostream>
#include <string>
#include <cstring>
#include <stack>
using namespace std;stack<char> c;char tar[1000];bool ans;void del()
{while (!c.empty()){c.pop();}
}bool equ(int t, char ss[], char s[])
{for (int i = 0; i<=t ; i++){if (s[i] != ss[i]){return false;}}return true;
}bool com()
{char b[10000]={'\0'};int i = 0;while (!c.empty()){b[i] = c.top();c.pop();i++;
//		cout<<"cbb "<<b[i]<<" this"<<endl;}//	cout<<"ans"<<tar<<" "<<b<<endl;if (equ(i, b, tar)){ans = true;//		cout<<"ans"<<tar<<" "<<b<<endl;return ans;}return false;
}int main()
{string a;getline(cin,a);int n = a.size();int x = n;char temp = a[n-1];while (temp != '='){n--;temp = a[n-1];
//		cout<<n<<endl;} for (int i = n, j = 0;i <= x-1; i++,j++){tar[j] = a[i];
//		cout<<"t"<<a[i]<<endl;}//提取目标字符串 //	cout<<"tar"<<tar<<endl;n--;//减去等号n--;//再减1直接拿n当下标n-=6;//减去target //	cout<<"a[n]"<<a[n]<<endl;while (a[n] != '='){if (a[n] == ']'){n--;continue;} if (a[n] == ',' || a[n] == '['){
//			cout<<"where"<<endl;if (!c.empty()){if (com()){break;}}n--;continue;}c.push(a[n]);//		cout <<"n"<< n<<" "<<a[n]<<" ";n--;} if (ans == true){cout<<"true";}else{cout<<"false";}return 0;
}

http://www.mrgr.cn/news/42217.html

相关文章:

  • mybatis如何与spring的结合
  • 【西门子V20变频器】 变频器运行时报A922报警
  • 15分钟学 Python 第34天 :小项目-个人博客网站
  • (十八)、登陆 k8s 的 kubernetes-dashboard 更多可视化工具
  • MySQL 中的 LAST_INSERT_ID()函数详解
  • MethodChanel的使用方法
  • Linux 应用层协议HTTP
  • Chapter04
  • python-数据容器
  • 【AI知识点】近似最近邻搜索(ANN, Approximate Nearest Neighbor Search)
  • Gazebo安装,ubuntu22
  • 卫生间门口墙皮天天掉,是墙面“返潮”造成的?
  • 国庆刷题(day2)
  • 【西门子V20变频器】如何设置面板控制
  • C++IO流
  • 写不出论文?分享7款写论文的ai免费工具网站
  • Linux聊天集群开发之环境准备
  • Resdis中关于字符串类型的基础命令
  • Django 后端数据传给前端
  • 如何选择与组合工具提升工作效率的秘密指南