英文:
How to find the correct brackets sequence?
问题
I've to figure it out that the bracket sequence is correct. Here is my problem.
Neat bracket
& Here is my solution code.
#include <stdio.h>
int main()
{
char s[100];
int c=0,count1=0,count2=0,count3=0;
scanf("%[^\n]",s);
while(s[c] !='#include <stdio.h>
int main()
{
char s[100];
int c=0,count1=0,count2=0,count3=0;
scanf("%[^\n]",s);
while(s[c] !='\0')
{
if(s[c] == '(')
{
++count1;
}
if(s[c] == ')')
{
++count2;
}
if(s[c] == '"')
{
++count3;
}
++c;
}
if(count1==count2 && count3%2 ==0)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
')
{
if(s[c] == '(')
{
++count1;
}
if(s[c] == ')')
{
++count2;
}
if(s[c] == '"')
{
++count3;
}
++c;
}
if(count1==count2 && count3%2 ==0)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
But it returns incorrect answer for a test case. & I also know that the algorithm is not proper 'cause it fails to give the correct answer for this test case
)))""(((
So how can I improve my algorithm???
英文:
I've to figure it out that the bracket sequence is correct. Here is my problem.
Neat bracket
& Here is my solution code.
#include <stdio.h>
int main()
{
char s[100];
int c=0,count1=0,count2=0,count3=0;
scanf("%[^\n]",s);
while(s[c] !='#include <stdio.h>
int main()
{
char s[100];
int c=0,count1=0,count2=0,count3=0;
scanf("%[^\n]",s);
while(s[c] !='\0')
{
if(s[c] == '(')
{
++count1;
}
if(s[c] == ')')
{
++count2;
}
if(s[c] == '"')
{
++count3;
}
++c;
}
if(count1==count2 && count3%2 ==0)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
')
{
if(s[c] == '(')
{
++count1;
}
if(s[c] == ')')
{
++count2;
}
if(s[c] == '"')
{
++count3;
}
++c;
}
if(count1==count2 && count3%2 ==0)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
But it returns incorrect answer for a test case. & I also know that the algorithm is not proper 'cause it fails to give the correct answer for this test case
> )))""(((
So how can I improve my algorithm???
答案1
得分: 1
使用std::stack来完成。思路是:
- 如果栈为空,将
s[i]
推入栈中。 - 比较当前字符(即
s[i]
)与stack.top()
,如果匹配,则弹出栈。 - 当找不到匹配时,将当前字符(即
s[i]
)推入栈。
假设输入是:"()(())"。现在进行逐步演示:
A. i = 0。最初栈为空。将"("推入栈。栈 - "("。
B. i = 1。栈不为空。比较"s1"即")"与栈顶的"("。它匹配。现在弹出栈。栈 - ""。
C. i = 2。现在栈为空。将"("推入栈。栈 - "("。
D. i = 3。栈不为空。比较"s[3]"即"("与栈顶的")"。它不匹配。现在将"("推入栈。栈 - "(("。
E. i = 4。栈不为空。比较"s[4]"即")"与栈顶的"("。它匹配。现在弹出栈。栈 - "("。
F. i = 5。栈不为空。比较"s[5]"即")"与栈顶的"("。它匹配。现在弹出栈。栈 - ""。
现在,栈为空,我们可以说字符串是整洁的 - 如问题中所述。如果栈中仍然存在未匹配的括号,那将意味着字符串不整洁。
// 假设s是包含括号序列的字符数组。
int i = 0;
std::stack<char> st;
while (s[i] != '// 假设s是包含括号序列的字符数组。
int i = 0;
std::stack<char> st;
while (s[i] != '\0') // 更好的选择是使用std::string
{
if (st.empty())
{
st.push(s[i++]);
continue;
}
if (st.top() == '(' && s[i] == ')')
{
st.pop();
i++;
}
else
st.push(s[i++]);
}
if (st.empty())
printf("Yes");
else
printf("No");
') // 更好的选择是使用std::string
{
if (st.empty())
{
st.push(s[i++]);
continue;
}
if (st.top() == '(' && s[i] == ')')
{
st.pop();
i++;
}
else
st.push(s[i++]);
}
if (st.empty())
printf("Yes");
else
printf("No");
英文:
Do it with std::stack. Idea is to
- If stack is empty, push
s[i]
to stack. - Compare current character aka
s[i]
withstack.top()
and pop stack if does match. - Push current character aka
s[i]
onto stack when no match is found.
Assume input is : "()(())". Now,
A. i = 0. Initially stack is empty. Push "(" on stack. Stack - "(".
B. i = 1. Stack is not empty. Compare ")" aka s1 with stack's top - "(". It does match. Now, pop stack. Stack - "".
C. i = 2. Now stack is empty. Push "(" on stack. Stack - "(".
D. i = 3. Stack is not empty. Compare "(" aka s[3] with stack's top - ")". It does not match. Now, push "(" to stack. Stack - "(("
E. i = 4. Stack is not empty. Compare ")" aka s[4] with stack's top - "(". It does match. Now, pop stack. Stack - "("
F. i = 5. Stack is not empty. Compare ")" aka s[5] with stack's top - "(". It does match. Now, pop stack. Stack - ""
Now, Stack is empty, We can say string is neat - as mentioned in question. Had we left with any unmatched parenthesis in stack, that'd mean string is not neat.
//Assuming s is the char array containing parenthesis sequence.
int i = 0;
std::stack<char> st;
while(s[i] != ' //Assuming s is the char array containing parenthesis sequence.
int i = 0;
std::stack<char> st;
while(s[i] != '\0') //Better choice would be to use std::string
{
if(st.empty() )
{
st.push( s[i++] );
continue;
}
if( st.top() == '(' && s[i] == ')' )
{ st.pop(); i++ }
else
st.push(s[i++]);
}
if(st.empty() )
printf("Yes");
else
printf("No");
') //Better choice would be to use std::string
{
if(st.empty() )
{
st.push( s[i++] );
continue;
}
if( st.top() == '(' && s[i] == ')' )
{ st.pop(); i++ }
else
st.push(s[i++]);
}
if(st.empty() )
printf("Yes");
else
printf("No");
答案2
得分: 0
好的。我已完成。这是我的解决方案。感谢 "max66"。
所以在这个问题中,你需要识别一件非常重要的事情,就是 ")" 没有与其闭合标签一起使用。这就是我已经做到的。
这是我的解决方案。
#include <stdio.h>
int main()
{
int c=0,count1=0,count2=0;
char s[1000];
scanf("%[^\n]",s);
while(s[c] != '好的。我已完成。这是我的解决方案。感谢 "max66"。
所以在这个问题中,你需要识别一件非常重要的事情,就是 ")" 没有与其闭合标签一起使用。这就是我已经做到的。
这是我的解决方案。
#include <stdio.h>
int main()
{
int c=0,count1=0,count2=0;
char s[1000];
scanf("%[^\n]",s);
while(s[c] != '\0')
{
if(s[c] == '"')
{
++count2;
}
if(s[c] == ')')
{
--count1;
}
if (count1 != -1)
{
if(s[c]== '(')
{
++count1;
}
}
++c;
}
///printf("%d\n",count1);
///printf("%d\n",count2);
if(count1 == 0 && count2%2 == 0)
{
printf("一切正常!\n");
}
else
{
printf("有些不对劲!\n");
}
return 0;
}
')
{
if(s[c] == '"')
{
++count2;
}
if(s[c] == ')')
{
--count1;
}
if (count1 != -1)
{
if(s[c]== '(')
{
++count1;
}
}
++c;
}
///printf("%d\n",count1);
///printf("%d\n",count2);
if(count1 == 0 && count2%2 == 0)
{
printf("一切正常!\n");
}
else
{
printf("有些不对劲!\n");
}
return 0;
}
英文:
Ok. I've done. Here is my solve. Thanks to " max66 ".
So in this problem , you've to identify one most important thing that ")" isn't used without its closing tags. That's I've done.
Here is my solution.
#include <stdio.h>
int main()
{
int c=0,count1=0,count2=0;
char s[1000];
scanf("%[^\n]",s);
while(s[c] != '#include <stdio.h>
int main()
{
int c=0,count1=0,count2=0;
char s[1000];
scanf("%[^\n]",s);
while(s[c] != '\0')
{
if(s[c] == '"')
{
++count2;
}
if(s[c] == ')')
{
--count1;
}
if (count1 != -1)
{
if(s[c]== '(')
{
++count1;
}
}
++c;
}
///printf("%d\n",count1);
///printf("%d\n",count2);
if(count1 == 0 && count2%2 == 0)
{
printf("Every thing is OK!\n");
}
else
{
printf("Something is fishy!\n");
}
return 0;
}
')
{
if(s[c] == '"')
{
++count2;
}
if(s[c] == ')')
{
--count1;
}
if (count1 != -1)
{
if(s[c]== '(')
{
++count1;
}
}
++c;
}
///printf("%d\n",count1);
///printf("%d\n",count2);
if(count1 == 0 && count2%2 == 0)
{
printf("Every thing is OK!\n");
}
else
{
printf("Something is fishy!\n");
}
return 0;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论