京东测试编程题

//1.字符串
#include<bits/stdc++.h>

using namespace std;

int main(){
	string s;

	cin >> s;

	int n = s.length();
	int i = n-1;
	while(s.substr(0,i) != s.substr(n-i,i))
		i--;
	cout << s.substr(0,n-i) << s << endl;

	return 0;

}

//2.括号题(AC 90%  暴力深搜  循环超时。。有没有优化思路)
#include<bits/stdc++.h>

using namespace std;

int res;

bool isvaild(string s){
	int n = s.length();
	if(n == 0)
		return true;
	if(n % 2 == 1)
		return false;
	stack<char> t;
	for(int i = 0;i < n;i++){
		if(s[i] == '(')
			t.push(s[i]);
		else if(s[i] == ')'){
			if(t.empty())
				return false;
			else
				t.pop();
		}
	}
	return true;
}



void dfs(string s){
	if(s.empty())
		res++;
	else{
		string temp(s.begin()+1,s.end());
		string st;
		for(int i = 0;i < temp.length();i++){
			if(temp[i] == ')'){
				st = temp;
				st.erase(st.begin()+i);
				if(isvaild(st)){
					dfs(st);
				}
			}
		}
	}
}

int main(){
	string s;
	cin >> s;
	res = 0;
	string t;
	dfs(s);

	cout << res << endl;

	return 0;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 10:39
一个证都没&nbsp;我能填什么
程序员小白条:别人有,你为什么没有,还是这个道理,社会就是比较,竞争,淘汰,你要安逸,那么就要做好淘汰的准备
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-08 13:15
点赞 评论 收藏
分享
嵐jlu:我是山川🐔里🐔🧱的,阿里系简历全过; 你这简历一看就还是半成品啊,没有荣誉经历奖项什么的吗?
投递阿里巴巴集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务