括号配对问题
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3
[(])
(])
([[]()])
样例输出
No
No
Yes
//讲输入的括号存在字符数组里面,定义一个栈s,遍历数组,如果数组元素与栈顶元素可匹配,出栈,否则入栈
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 10000 + 5;
char a[maxn];
int main(){
int T;
scanf("%d",&T);
while(T--)
{
scanf("%s",a);
int len = strlen(a);
stack<char>s;
for(int i = 0; i < len; i++)
{
if(a[i] == ']'&&s.size()&&s.top()=='['){
s.pop();
}else if(a[i] == ')'&&s.size()&&s.top()=='('){
s.pop();
}else{
s.push(a[i]);
}
}
if(s.size())
{
printf("No\n");
}else{
printf("Yes\n");
}
}
return 0;
}
1. stack
stack 模板类的定义在头文件中。
stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要
的,在不指定容器类型时,默认的容器类型为deque。
定义stack 对象的示例代码如下: stack<int> s1;
stack<string> s2;
stack 的基本操作有:
入栈,如例:s.push(x);
出栈,如例:s.pop();
注意,出栈操作只是删除栈顶元素,并不返回该元素。
访问栈顶,如例:s.top()
判断栈空,如例:s.empty()
,当栈空时,返回true。
访问栈中的元素个数,如例:s.size().
2. queue
queue 模板类的定义在<queue>
头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下: queue<int> q1;
queue<double> q2;
queue 的基本操作有:
入队,如例:q.push(x);
将x 接到队列的末端。
出队,如例:q.pop();
弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front()
,即最早被压入队列的元素。
访问队尾元素,如例:q.back()
,即最后被压入队列的元素。
判断队列空,如例:q.empty()
,当队列空时,返回true。
访问队列中的元素个数,如例:q.size()
ASCII码排序
时间限制:3000 ms | 内存限制:65535 KB
难度:2
描述
输入三个字符(可以重复)后,按各字符的ASCII码从小到大的顺序输出这三个字符。
输入
第一行输入一个数N,表示有N组测试数据。后面的N行输入多组数据,每组输入数据都是占一行,有三个字符组成,之间无空格。
输出
对于每组输入数据,输出一行,字符中间用一个空格分开。
样例输入
2
qwe
asd
样例输出
e q w
a d s
利用sort排序
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
char a[100];
scanf("%s",a);
int length = strlen(a);
sort(a,a+length);
for(int i = 0; i < length; i++)
{
printf("%c ",a[i]);
}
printf("\n");
}
}
Binary String Matching
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
Given two strings A and B, whose alphabet consist only ‘0’ and ‘1’. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is ‘1001110110’ while the pattern string A is ‘11’, you should output 3, because the pattern A appeared at the posit
输入
The first line consist only one integer N, indicates N cases follows. In each case, there are two lines, the first line gives the string A, length (A) <= 10, and the second line gives the string B, length (B) <= 1000. And it is guaranteed that B is always longer than A.
输出
For each case, output a single line consist a single integer, tells how many times do B appears as a substring of A.
样例输入
3
11
1001110110
101
110010010010001
1010
110100010101011
样例输出
3
0
3
#include<iostream>
#include<string>
#include<stdio.h>
using namespace std;
int main()
{
int N;
scanf("%d",&N);
while(N--)
{
string a, b;
cin>>a>>b;
int count = 0;
int position = b.find(a,0);//find 函数 返回jk 在s 中的下标位置
while(position!=string::npos)
{
count++;
position = b.find(a,position+1);
}
printf("%d\n",count);
}
return 0;
}
http://www.cnblogs.com/web100/archive/2012/12/02/cpp-string-find-npos.html
- 题目8
- 题目信息