10 solutions
- 1
Information
- ID
- 292
- Time
- 1000ms
- Memory
- 128MiB
- Difficulty
- 6
- Tags
- # Submissions
- 200
- Accepted
- 58
- Uploaded By
#include<bits/stdc++.h>
using namespace std;
int main()
{
char i[105];
while(scanf("%s",i)!=EOF){
int sum=0;
char a[105];
memset(a,0,sizeof(a));
int k=strlen(i);
stack<int> pp;
for(int c=0;c<k;c++){
cout<<i[c];
if(i[c]=='('){
pp.push(i[c]);
a[c]='(';
}else if(i[c]==')'){
if(pp.empty()){
a[c]='?';
}else{
a[c]=')';
pp.pop();
}
}else{
a[c]=' ';
}
}
cout<<endl;
for(int c=k-1;c>=0;c--){
if(a[c]==')'){
sum++;
a[c]=' ';
}else if(a[c]=='('){
if(sum==0){
a[c]='$';
}else{
a[c]=' ';
if(sum>0) sum--;
}
}
}
for(int c=0;c<k;c++){
cout<<a[c];
}
cout<<endl;
}
return 0;
}
先用字符串把要输入的东西保存下来,再用stack一个一个的分析括号匹配,做个模板,如果不能匹配就记录下来,注意最后getchar把每次的回车字符去掉
#include <bits/stdc++.h>
using namespace std;
int main(){
char str[107],mo[107];
stack <int> s;
while(scanf("%s",&str))
{
int l=strlen(str);
for(int i=0;i<l;i++)
{
if(str[i]=='(')
{
s.push(i);
mo[i]=' ';
}
else if(str[i]==')')
{
if(!s.empty())
{
s.pop();
mo[i]=' ';
}
else
{
mo[i]='?';
}
}
else
{
mo[i]=' ';
}
}
while(!s.empty())
{
mo[s.top()]='$';
s.pop();
}
puts(str);
for(int i=0;i<l;i++)
{
printf("%c",mo[i]);
}
cout<<endl;
getchar();
}
return 0;
}
思想就是用一个stack进行配对,再用另一个进行对位置的存储,如果配对成功就删除记录的位置,最后用数组T来记录属于那种配对不成功,然后再进行输出 写得一般,仅供参考
#include <bits/stdc++.h>
using namespace std;
stack<char>s;stack<int> ss;
int T[105];
int main(){
string k;
while(cin>>k){//输入
memset(T,0,sizeof(T));//格式化,防止上次数据的影响
int kk=k.length();
for(int i=0;i<kk;i++){
if(k[i]=='(') s.push(k[i]),ss.push(i);//左括号直接压进去
else if(k[i]==')'){
if(!(s.empty())&&s.top()=='('){//配对成功就删除
ss.pop(),s.pop();
}
else s.push(k[i]),ss.push(i);//没成功就压进去
}
}
int kkk=s.size();
for(int i=0;i<kkk;i++){
if(s.top()=='(') T[ss.top()]=1;//如果左括号没配的成功就是1
else if(s.top()==')') T[ss.top()]=2;//如果右括号没配的成功就是2
s.pop(),ss.pop();//清除已经记录下的数据
}
cout<<k<<endl; //按题目要求输出
for(int i=0;i<kk;i++){
if(T[i]==1) cout<<'$';
else if(T[i]==2) cout<<'?';
else cout<<' ';
}
cout<<endl;
}
return 0;
}
我是蒟蒻,不会写题解,所以。。。
#include<bits/stdc++.h>
using namespace std;
char arr[107];
char brr[107];
stack<int> s;
int main()
{
while(cin>>arr)
{
int l=strlen(arr);
for(int i=0;i<l;i++)
{
if(arr[i]=='(')
{
s.push(i);
brr[i]=' ';
}
else if(arr[i]==')')
{
if(!s.empty())
{
s.pop();
brr[i]=' ';
}
else
{
brr[i]='?';
}
}
else
{
brr[i]=' ';
}
}
while(!s.empty())
{
brr[s.top()]='$';
s.pop();
}
cout<<arr<<endl;
cout<<brr<<endl;
memset(brr,0,sizeof(brr));
memset(arr,0,sizeof(arr));
}
return 0;
}
我是蒟蒻
#include<bits/stdc++.h>
using namespace std;
stack <int> s;
string a;
char b[100];
int main(){
while(cin>>a){
memset(b,' ',100);//初始化一个字符数组
for(int i=0;i<a.size();i++){
if(a[i]=='('){//如果是(就压入坐标
s.push(i);
}
else if(a[i]==')'){
if(!s.empty()&&a[s.top()]=='('){//如果是)需要判断他上一个是不是(
s.pop();
}
else{
s.push(i);
}
}
}
cout<<a<<endl;
while(!s.empty()){
if(a[s.top()]=='('){
b[s.top()]='$';
s.pop();
}
else if(a[s.top()]==')'){
b[s.top()]='?';
s.pop();
}
}
cout<<b<<endl;
}
return 0;
}
#include<bits/stdc++.h>
#include<string.h>
#define ll long long
using namespace std;
int cnt,ans;
char num[105];
int main()
{
std::ios::sync_with_stdio(false);
while(~scanf("%s",num))
{
stack<int> st1;//记录‘(’ 的位置
stack<int> st2;//记录‘)’ 的位置
for(int i=0;i<=strlen(num);i++)
{
if(num[i]=='(')
st1.push(i);
if(num[i]==')')
{
if(!st1.empty())//如果非空
st1.pop();
else
st2.push(i);
}
}
cout<<num<<endl;//先输出原字符串
while(!st1.empty()){//改变原来的字符串把未匹配的‘(’换成‘$’
num[st1.top()]='$';//获得栈中的最顶端的元素
st1.pop();//每次用完后都要出栈
}
while(!st2.empty()){//改变原来的字符串把未匹配的‘)’换成‘?’
num[st2.top()]='?';//获得栈中的最顶端的元素
st2.pop();//每次用完后都要出栈
}
for(int i=0;i<strlen(num);i++)//输出
{
if(num[i]=='?'||num[i]=='$')
cout<<num[i];
else
cout<<" ";
}
cout<<endl;
}
return 0;
}
那个啥,我蒟蒻
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100];
int l[100],r[100],ll[100];
int left,right;
while(~scanf("%s",arr)){
left=0,right=0;
for(int i = 0;i<strlen(arr);i++){
l[i]=0;
r[i]=0;
ll[i]=0;
}
for(int i = 0;i <strlen(arr);i++){
if(arr[i]=='('){
left++;
l[left] = i;
}
if(arr[i]==')'){
if(left==0){
r[i] = 1;
}
else{
l[left] = 0;
left--;
}
}
}
if(left!=0){
for(int j = 1;j<=left;j++){
ll[l[j]] = 1;
l[j]=0;
}
}
printf("%s\n",arr);
for(int i = 0;i<strlen(arr);i++){
if(r[i]==1)printf("?");
else if(ll[i]==1){
printf("$");
}
else printf(" ");
ll[i]=0;
r[i]=0;
}
memset(arr,0,100);
printf("\n");
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
stack<int> s;
string str;
char a[1005];
int main()
{
while(cin>>str)
{
memset(a,' ',101);//初始化
for(int i=0;i<str.size();++i)
{
if(str[i]=='(') s.push(i);
if(str[i]==')')
{
if(s.empty())
{
a[i]='?';
}
else
{
s.pop();//抵消一个 (
}
}
}
while (!s.empty())//不为空说明 有多余的 (
{
a[s.top()] = '$';//从栈顶开始记录
s.pop();//删除一个
}
cout<<str<<"\n";
cout<<a<<"\n";
}
return 0;
}
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
char s[100010];
char a[100010];
stack<char> q;//一个记录符号
stack<int> q2;一个记录位置
int main()
{
while(scanf("%s",s)!=EOF)//多组输入;
{
int flage=0;
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='('||s[i]==')')
{
if(flage==0)
{
q.push(s[i]);
q2.push(i);
flage=1;
}
else if((s[i]==')'&&q.top()=='('))
{
a[q2.top()]=' ';
a[i]=' ';
q.pop();
q2.pop();
if(q.empty()) flage=0;
}
else{
q.push(s[i]);
q2.push(i);
}
}
else a[i]=' ';
}
while(!q.empty())
{
char ch=q.top();
if(ch=='(')
{
a[q2.top()]='$';
q.pop();
q2.pop();
}
else {
a[q2.top()]='?';
q.pop();
q2.pop();
}
}
for(int i=0;i<strlen(s);i++) cout<<s[i];
cout<<endl;
for(int i=0;i<strlen(s);i++) cout<<a[i];
cout<<endl;
memset(s,0,sizeof(s));//初始化
memset(a,0,sizeof(a));
}
return 0;
}
By signing up a 追梦算法网 universal account, you can submit code and join discussions in all online judging services provided by us.