ACM-括号配对问题

括号配对问题

题目要求

描述 现在,有一行括号序列,请你检查这行括号是否配对。 输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No


代码展示:

01. #include <iostream>
02. #include <cstring>
03. using namespace std;
04. int main()
05. {
06. int t;
07. cin>>t;
08. while(t--)
09. {
10. string str;
11. cin>>str;
12. for(int i=0;i<str.size();i++)
13. {
14. if(str[i]==str[i+1]-1||str[i]==str[i+1]-2){str.erase(i,2);i=-1;}
15. }
16. if(str.size())cout<<"No"<<endl;
17. else
18. cout<<"Yes"<<endl;
19. }
20. }

这里需要大家掌握一个循环:

//这个循环再今后的做题当中用到的很多,很方便

while(t--)

{

}

str.size()测字符串长度(此处为转载)

一:
#include
using namespace std;
int main()
{
string name1 = "youku";    
const char* name2 = "youku";  
// char name3[] ={'y','o','u','k','u','\0'}; 
char name3[] = "youhu"; 
size_t l1 = name1.size();     //5
size_t l2 = strlen(name2);   //5
size_t l3 = sizeof(name2);   //4
size_t l4 = sizeof(name3);   
size_t l5 = strlen(name3);   
cout<<l1<<" "<<l2<<""<<l3<<" "<<l4<<""<<l5<<endl;
// printf("%d:%d:%d:%d:%d",l1,l2,l3,l4,l5);
return 0;
}
输出:5 5 4 6 5
二:
#include
using namespace std;
int main()
{
string name1 = "youku";   
const char* name2 = "youku"; 
char name3[] ={'y','o','u','k','u'}; 
// char name3[] ="youhu"; 
size_t l1 = name1.size();    //5
size_t l2 = strlen(name2);  //5
size_t l3 = sizeof(name2);  //4
size_t l4 = sizeof(name3);   
size_t l5 = strlen(name3);   
cout<<l1<<""<<l2<<" "<<l3<<" "<<l4<<""<<l5<<endl;
//printf("%d:%d:%d:%d:%d",l1,l2,l3,l4,l5);
return 0;
}
输出:5 5 4 5 11



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值