Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

C++会编译错误,G++秒过

Posted by KatrineYang at 2016-09-16 04:01:52 on Problem 1335
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

void print(string s){
	int len = s.length();
	for(int i = 0; i < len; i++) printf("%c ", s[i]);
	printf("$\n");
}

bool isLast(string s){
	int len = s.length();
	for(int i = 0; i < len/2; i++){
		if(s[i] == ')') return 0;
	}
	return 1;
}

int findBPos(string s){
	int kh = 1, len = s.length();
	for(int i = 1; i < len; i++){
		if(s[i] == '(') kh++;
		else{
			kh--;
			if(kh == 0) return i+1;
		}
	}
	return len;
}

string kuohao(int n){
	string s = "";
	for(int i = 0; i < n; i++) s += "()";
	return s;
}

string next(string s){
	if(!s.length()) return "()";
	int bpos = findBPos(s);
	string B = s.substr(bpos), khA = s.substr(0, bpos);
	string A = khA.substr(1, khA.length()-2);
	if(!isLast(B)){
		return khA + next(B);
	}
	else if(!isLast(A)){
		return "(" + next(A) + ")" + kuohao((B.length()/2));
	}
	else if(B.length()){
		return "(" + kuohao(A.length()/2+1) + ")" + kuohao(B.length()/2-1);
	}
	else{
		return kuohao(s.length()/2+1);
	}
}

int main() {
	int T;
	cin >> T;
	for(int i = 0; i < T; i++){
		//cout << 1 << endl;
		char c;
		char str[80] = {'\0'};
		int len = 0;
		while(1){
			cin >> c;
			if(c == '$') break;
			str[len] = c;
			len++;
		}
		string DO(str);
		print(next(DO));
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator