关于括号匹配的一些问题总结
2013-08-25 18:06
309 查看
1 九度OJ 1153
使用栈简单的匹配
http://ac.jobdu.com/problem.php?pid=1153
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
int main(){
char str[100];
char out[100];
stack<int> s;
while(scanf("%s",str) != EOF){
int n = strlen(str);
memset(out,' ',sizeof(out));
for(int i=0; i<n; i++){
if(str[i] == '('){
s.push(i);
}else if(str[i] == ')'){
if(s.size() > 0)
s.pop();
else
out[i] = '?';
}
}
while(s.size() > 0){
int k = s.top();
s.pop();
out[k] = '$';
}
out
= '\0';
printf("%s\n%s\n",str,out);
}
return 0;
}
2. 九度 1337
找最长的合法字串
使用数组模拟栈
#include <stdio.h>
char s[1000001];
int opt[1000001];
int stack[1000000], m, ans, l, cnt, i, last, ll;
int main() {
int n;
while (scanf("%d %s", &n, s) != EOF) {
opt[0] = opt[1] = 0;
cnt = l = ans = m = last =0;
for (i = 1; i <= n; i++) {
if (s[i-1] == '(') {
stack[l++] = i;
opt[i] = 0;
} else {
if(l){
l--;
opt[i] = opt[stack[l]-1] + 2;
if(s[i-2] == ')')
opt[i] += opt[i-1];
opt[stack[l] - 1] = opt[i];
}else
opt[i] = 0;
}
if(opt[i] > ans){
ans = opt[i];
cnt = 1;
}else if(opt[i] == ans) cnt ++;
}
if(ans)
printf("%d %d\n", ans, cnt);
else
printf("0 1\n");
}
return 0;
}
3. 九度 1342
#include <stdio.h>
char s[1000001];
int i, l, r, ans;
int main(){
while (scanf("%s",s) != EOF) {
l = ans = 0;
for(i=0; s[i]; i++){
if(s[i] == '('){
l++;
}else{
if(l){
ans += 2;
l--;
}
}
}
printf("%d\n", ans);
}
return 0;
}
使用栈简单的匹配
http://ac.jobdu.com/problem.php?pid=1153
#include <stdio.h>
#include <stack>
#include <string.h>
using namespace std;
int main(){
char str[100];
char out[100];
stack<int> s;
while(scanf("%s",str) != EOF){
int n = strlen(str);
memset(out,' ',sizeof(out));
for(int i=0; i<n; i++){
if(str[i] == '('){
s.push(i);
}else if(str[i] == ')'){
if(s.size() > 0)
s.pop();
else
out[i] = '?';
}
}
while(s.size() > 0){
int k = s.top();
s.pop();
out[k] = '$';
}
out
= '\0';
printf("%s\n%s\n",str,out);
}
return 0;
}
2. 九度 1337
找最长的合法字串
使用数组模拟栈
#include <stdio.h>
char s[1000001];
int opt[1000001];
int stack[1000000], m, ans, l, cnt, i, last, ll;
int main() {
int n;
while (scanf("%d %s", &n, s) != EOF) {
opt[0] = opt[1] = 0;
cnt = l = ans = m = last =0;
for (i = 1; i <= n; i++) {
if (s[i-1] == '(') {
stack[l++] = i;
opt[i] = 0;
} else {
if(l){
l--;
opt[i] = opt[stack[l]-1] + 2;
if(s[i-2] == ')')
opt[i] += opt[i-1];
opt[stack[l] - 1] = opt[i];
}else
opt[i] = 0;
}
if(opt[i] > ans){
ans = opt[i];
cnt = 1;
}else if(opt[i] == ans) cnt ++;
}
if(ans)
printf("%d %d\n", ans, cnt);
else
printf("0 1\n");
}
return 0;
}
3. 九度 1342
#include <stdio.h>
char s[1000001];
int i, l, r, ans;
int main(){
while (scanf("%s",s) != EOF) {
l = ans = 0;
for(i=0; s[i]; i++){
if(s[i] == '('){
l++;
}else{
if(l){
ans += 2;
l--;
}
}
}
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- 关于VMware不完全卸载的一些问题的总结
- 关于string类中的一些小问题的总结
- 【关于ASP.NET在IIS一些问题的经验总结】
- 【实践】总结(2015/05/07)——关于布局和兼容的一些问题
- 关于\r和\n的一些问题总结
- 关于hibernate 1+n问题的一些总结
- 这是我们公司总结的一些关于中文乱码问题的一些解决方案和经验和大家分享!
- 关于崩溃等问题的定位总结(使用qcc的一些方法来尝试)
- 关于括号匹配问题
- 关于\r和\n的一些问题总结
- 最近在ArcGIS Engine开发中关于调用gp工具过程出现COM 组件的调用返回了错误 HRESULT E_FAIL 错误的解决方法 和 学习oracle中遇到的一些问题总结
- 关于手机的IMEI号问题的一些总结
- 总结一些关于操作数据库是sql语句还是存储过程问题
- 关于偏序问题的一些总结
- 关于IIS在使用中遇到的一些问题的总结
- 关于一道括号匹配问题
- 关于百度地图的一些问题总结
- 关于ASP.NET在IIS一些问题的经验总结
- 关于在部署KMS过程中的一些问题和总结(windows 7)
- 关于左值和右值的一些问题总结