该部分题目全部来自南阳理工学院的OJ。
地址:http://acm.nyist.net/JudgeOnline/problemset.php
只是简单记录一下,不按照全部顺序进行记录。
题目如下:
描述:
现在,有一行括号序列,请你检查这行括号是否配对。
输入:
第一行输入一个数N(0< N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有”[“,”]”,”(“,”)”四种字符
输出:
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef unsigned int uint32_t;
void bMatchStr(char* str1, uint32_t size) {
//括号数为偶数
if (size % 2 != 0 || size == 0) {
printf("No\n");
return;
}
//最大为1万,则栈至少为5000
char pLeft[5000] = { 0 };
//栈指针
uint32_t uLeftIndex = 0;
for (int i = 0; i < size; ++i) {
//压栈
*(pLeft + uLeftIndex) = str1[i];
//根据字符的ASCII码进行比较
if ((*(pLeft + uLeftIndex) - 1 == *(pLeft + uLeftIndex - 1) )
|| (*(pLeft + uLeftIndex) - 2 == *(pLeft + uLeftIndex - 1))) {
uLeftIndex -= 1; //匹配成功出栈
} else {
uLeftIndex += 1; //匹配失败栈顶指针+1
}
}
if (uLeftIndex == 0)
printf("Yes\n");
else
printf("No\n");
return;
}
int main(int argc, char** argv) {
unsigned int nCloum = 0;
scanf("%d", &nCloum);
int i = 0;
char buff[10000] = { 0 };
while (i++ < nCloum){
scanf("%s", buff);
bMatchStr(buff, strlen(buff));
}
return 0;
}
//缺少异常操作处理
360

被折叠的 条评论
为什么被折叠?



