Live Archive 训练题 2019/3/9

7454 Parentheses

A bracket is a punctuation mark, which is used in matched pairs, usually used within articles or programs. Brackets include round brackets, square brackets, curly brackets, angle brackets, and various other pairs of symbols. Let’s focus on the round brackets, also called parentheses. A sequence of parentheses is said to be well-formed if the parentheses are properly nested. For example, A = a1a2 . . . a18 = “(()())()()()(())()” is well-formed, but B = b1b2 . . . b18 = “(()())))(((((())((” is not. (See Figure 1.) More formally, a sequence of parentheses P = p1p2 . . . pn is well-formed

if (1) when scanning it from p1 to pn, the number of right parentheses does not exceed the number of left parentheses at any state, and

(2) the numbers of left and right parentheses are equal.

Figure 1. Two sequences of parentheses.AutoText is a company, which is developing a text editor for programmers.  The new editor willprovide many powerful functions to automatically correct typing errors. On a keyboard, the left andright parentheses are adjacent. Thus, it is often that “)” is mistyped as “(” or vice versa. And therefore,one of the functions AutoText wants to provide is to automatically convert a sequence of parenthesesP(that may not be well-formed) into a wellformed sequenceP′. In the conversion, the only allowedoperation is to reverse a parenthesis (i.e., either to replace a “(” with a “)” or to replace a “)” witha “(”). For example, in Figure 1, we can convertBinto the well-formed sequenceAby performing 4reverse operations onb7,b10,b12,b18. Of course, there may be several ways to convert a sequence intoa well-formed sequence. A conversion is optimal if it uses the minimum number of reverse operations.Please write a program to compute the minimum number of reverse operations that make a givensequence of parenthesesP=p1p2:::pnwell-formed.InputThe first line contains an integerT10indicating the number of test cases. The first line of each testcase contains an even integern,2n100, indicating the length ofP. Next, the second line givesthe sequenceP.

Output

For each test case, output the minimum number of reverse operations that makePwell-formed.

Sample Input

3

18

(()())))(((((())((

2

()

8

(()))()(

Sample Output

4

0

2

题目意思:对于给出的一系列括号,设计一个AutoText,使得能够自动完成括号匹配,问最少需要转变多少个括号,左括号和右括号可以相互转变。

解题思路:之前经常做这种括号匹配的题目,使用栈先将能够匹配的处理掉,之后栈内不能匹配的两个处理,使其能够匹配即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
using namespace std;
stack<char>s;
int main()
{
    int t,n,ans;
    int x,y;
    char c;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        getchar();
        ans=0;
        while(!s.empty())///清空栈
        {
            s.pop();
        }
        while(n--)
        {
            scanf("%c",&c);
            if(c=='(')
            {
                s.push(c);
            }
            else if(c==')')
            {
                if(!s.empty()&&s.top()=='(')///已经匹配出栈
                {
                    s.pop();
                }
                else
                {
                    s.push(c);
                }
            }
        }
        x=0;
        y=0;
        ans=0;
        while(!s.empty())
        {
            if(s.top()=='(')
            {
                x++;
            }
            else if(s.top()==')')
            {
                y++;
            }
            if(x==1&&y==1)///出现')('的情况
            {
                x--;
                y--;
                ans=ans+2;///两个同时翻转
            }
            else if(x==2)///出现'(('的情况
            {
                x=x-2;
                ans++;///翻转一个
            }
            else if(y==2)///出现'))'的情况
            {
                y=y-2;
                ans++;///翻转一个
            }
            s.pop();
        }
        printf("%d\n",ans);
    }
    return 0;
}

7457Discrete Logarithm Problem

题目意思:求一个x使得 a^x%p = b 。

解题思路:开始一看这种表达式,第一反应是快速幂算法,但后来发现数据的规模并不是很大,所以可以直接暴力枚举,同时发现而取模的结果一定在p次以内出现循环,所以直接从0~p枚举x即可。

#include<cstdio>
#include<cstring>
#define ll long long int
#define maxs 102400
using namespace std;
/*ll fast_pow(ll a,ll x,ll p)
{
    ll ans=1;
    a=a%p;
    while(x!=0)
    {
        if(x&1)
        {
            ans=(ans*a)%p;
        }
        x>>=1;
        a=(a*a)%p;
    }
    return ans%p;
}*/
//快速幂
int main()
{
    ll a,b,p,ans,i;
    int flag;
    scanf("%lld",&p);
    while(scanf("%lld",&a)!=EOF)
    {
        if(a==0)
        {
            break;
        }
        scanf("%lld",&b);
        ans=a;
        flag=0;
        for(i=2;i<p;i++)
        {
            ans=(ans*a)%p;
            if(ans==b)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            printf("%d\n",i);
        }
        else
        {
            printf("0\n");
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/wkfvawl/p/10504895.html