数据结构与算法实验-实验三:栈应用(1)

栈应用(1)

这个题题目变了!!!

文章目录

    • 栈应用(1)
      • 这个题题目变了!!!
      • 旧题目
      • 新题目

旧题目

用栈实现:输入一行符号,以#结束,判断其中的括号是否匹配。括号包括:
{ } 、 [ ] 、 ( )、 < >
如果匹配,输出 right
如果不匹配,给出错误提示。包括:
(1)对称符号都匹配,输出 “ right “
(2)处理到某个符号时不匹配了,输出 " The $ character ‘*’ is wrong." ,其中$是出错符号的序号,*是出错的符号;
(3)如果有没匹配的左符号,输出缺失的对应的右符号,即输出 “loss of right character 。。。.”,其中。。。是按嵌套顺序对应的右符号。
例如:
样例输入1:
as(*x<{(({<>}))}>)#
样例输出1:
right
样例输入2:
(a.b)>#
样例输出2:
The 6 character ‘>’ is wrong.
样例输入3:
({()#
样例输出3:
loss of right character }).
括号匹配模板题。

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 2000005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set::iterator
#define fs(n) fixed<< setprecision(n)
const double e=2.71828182845;
const double pi = acos(-1.0);
void read(int &x)
{
    char ch=getchar();
    x=0;
    for(; ch<'0'||ch>'9'; ch=getchar());
    for(; ch>='0'&&ch<='9'; x=x*10+ch-'0',ch=getchar());
}
inline void write(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    printf("\n");
}
float SqrtByCarmack( float number )
{
    int i;
    float x2, y;
    const float threehalfs = 1.5F;
    x2 = number * 0.5F;
    y  = number;
    i  = * ( int * ) &y;
    i  = 0x5f375a86 - ( i >> 1 );
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    return number*y;
}
ll qpow(ll a,ll b,ll mod)
{
    ll sum=1;
    while(b)
    {
        if(b%2==1)
        {
            sum=sum*a%mod;
        }
        b/=2;
        a=a*a%mod;
    }
    return sum;
}
int erfen(int *a,int start,int endd,int l)//小于等于l的最大值的角标
{
    int mid=(start+endd)/2;
    if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))
        return mid;
    else if(a[mid]<=l)
        return erfen(a,mid+1,endd,l);
    else if(a[mid]>l)
    {
        if(start!=mid)
            return erfen(a,start,mid,l);
        else
            return start-1;
    }
}
ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod)
{
    return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
}
bool Miller_Rabin(ll p)
{
    if(p < 2)
        return 0;
    if(p != 2 && p % 2 == 0)
        return 0;
    ll s = p - 1;
    while(! (s & 1))
        s >>= 1;
    for(int i = 0; i < 5; ++i)
    {
        if(p == prime[i])
            return 1;
        ll t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1)
        {
            m = qmul(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1))
            return 0;
    }
    return 1;
}
ll gcd(ll x,ll y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
stack<char>st;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string str;
    cin>>str;
    int len=str.size();
    int flag=0;
    rep(i,0,len-1)
    {
        if(str[i]=='{'||str[i]=='['||str[i]=='('||str[i]=='<')
        {
            st.push(str[i]);
        }
        else
        {
            if(str[i]=='}'||str[i]==']'||str[i]==')'||str[i]=='>')
            {
                if(st.empty())
                {
                    cout<<"The "<<i+1<<" character '"<<str[i]<<"' is wrong.";
                    flag=1;
                    break;
                }
                char cha=st.top();
                if((str[i]=='}'&&cha=='{')||(str[i]==']'&&cha=='[')||(str[i]==')'&&cha=='(')||(str[i]=='>'&&cha=='<'))
                    st.pop();
                else
                {
                    cout<<"The "<<i+1<<" character '"<<str[i]<<"' is wrong.";
                    flag=1;
                    break;
                }
            }
        }
    }
    if(flag==1)
        return 0;
    if(st.empty())
        cout<<"right";
    else
    {
        cout<<"loss of right character ";
        while(!st.empty())
        {
            if(st.top()=='{')
                cout<<"}";
            if(st.top()=='(')
                cout<<")";
            if(st.top()=='[')
                cout<<"]";
            if(st.top()=='<')
                cout<<">";
            st.pop();
        }
        cout<<".";
    }
    return 0;
}

新题目

用栈ADT应用:对称符号匹配判断
输入一行符号,以#结束,判断其中的对称符号是否匹配。对称符号包括:
{ } 、 [ ] 、 ( )、 < >
输出分为以下几种情况:

    (1)对称符号都匹配,输出 “ right. “
      (2)  如果处理到最后出现了失配,则输出两行:
            第一行:Matching failure.
            第二行:loss of right character $$....        其中$$... 是按嵌套顺序对应的右匹配符号。
    (3)处理到某个符号时失配了,则输出两行或三行:
            第一行: The N character '$' is wrong." ,其中N是出错符号的序号,$是出错的符号;
            第二行:  loss of left character $.”      其中 $ 是当前符号的左匹配符号。   
            (如果有的话)第三行:loss of right character $$...”   其中$$... 是按嵌套顺序对应的右匹配符号。

例如:
输入
(a.b)>#
输出:
The 6 character >’ is wrong.
loss of left character <.

输入 :
({()#
输出:
Matching failure.
loss of right character }).

样例输入1:
as(*x<{(({<>}))}>)#
样例输出1:
right.
样例输入2:
(a.b)>#
样例输出2:
The 6 character ‘>’ is wrong.
loss of left character <.

这个题改的。。。哎,再给大家提供一组数据吧,估计这组能卡好多人。
输入:
((((}
输出:
The 5 character ‘}’ is wrong.
loss of left character {.
loss of right character )))).
代码如下:

#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define per(i,l,r) for(ll i=r;i>=l;i--)
#define eif else if
#define N 2000005
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
#define IT set::iterator
#define fs(n) fixed<< setprecision(n)
const double e=2.71828182845;
const double pi = acos(-1.0);
void read(int &x)
{
    char ch=getchar();
    x=0;
    for(; ch<'0'||ch>'9'; ch=getchar());
    for(; ch>='0'&&ch<='9'; x=x*10+ch-'0',ch=getchar());
}
inline void write(int x)
{
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
    printf("\n");
}
float SqrtByCarmack( float number )
{
    int i;
    float x2, y;
    const float threehalfs = 1.5F;
    x2 = number * 0.5F;
    y  = number;
    i  = * ( int * ) &y;
    i  = 0x5f375a86 - ( i >> 1 );
    y  = * ( float * ) &i;
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    y  = y * ( threehalfs - ( x2 * y * y ) );
    return number*y;
}
ll qpow(ll a,ll b,ll mod)
{
    ll sum=1;
    while(b)
    {
        if(b%2==1)
        {
            sum=sum*a%mod;
        }
        b/=2;
        a=a*a%mod;
    }
    return sum;
}
int erfen(int *a,int start,int endd,int l)//小于等于l的最大值的角标
{
    int mid=(start+endd)/2;
    if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))
        return mid;
    else if(a[mid]<=l)
        return erfen(a,mid+1,endd,l);
    else if(a[mid]>l)
    {
        if(start!=mid)
            return erfen(a,start,mid,l);
        else
            return start-1;
    }
}
ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod)
{
    return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
}
bool Miller_Rabin(ll p)
{
    if(p < 2)
        return 0;
    if(p != 2 && p % 2 == 0)
        return 0;
    ll s = p - 1;
    while(! (s & 1))
        s >>= 1;
    for(int i = 0; i < 5; ++i)
    {
        if(p == prime[i])
            return 1;
        ll t = s, m = qpow(prime[i], s, p);
        while(t != p - 1 && m != 1 && m != p - 1)
        {
            m = qmul(m, m, p);
            t <<= 1;
        }
        if(m != p - 1 && !(t & 1))
            return 0;
    }
    return 1;
}
ll gcd(ll x,ll y)
{
    if(y==0)
        return x;
    else
        return gcd(y,x%y);
}
stack<char>st;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string str;
    //{ } 、 [ ] 、 ( )、  < >
    cin>>str;
    int len=str.size();
    int flag=0;
    rep(i,0,len-1)
    {
        //cout<
        if(str[i]=='{'||str[i]=='['||str[i]=='('||str[i]=='<')
        {
            st.push(str[i]);
        }
        else
        {
            if(str[i]=='}'||str[i]==']'||str[i]==')'||str[i]=='>')
            {
                if(st.empty())
                {
                    cout<<"The "<<i+1<<" character '"<<str[i]<<"' is wrong."<<'\n';
                    flag=1;
                    if(str[i]=='}')
                        cout<<"loss of left character {."<<'\n';
                    if(str[i]==')')
                        cout<<"loss of left character (."<<'\n';
                    if(str[i]==']')
                        cout<<"loss of left character [."<<'\n';
                    if(str[i]=='>')
                        cout<<"loss of left character <."<<'\n';
                    break;
                }
                char cha=st.top();
                if((str[i]=='}'&&cha=='{')||(str[i]==']'&&cha=='[')||(str[i]==')'&&cha=='(')||(str[i]=='>'&&cha=='<'))
                    st.pop();
                else
                {
                    cout<<"The "<<i+1<<" character '"<<str[i]<<"' is wrong."<<'\n';
                    flag=1;
                    if(str[i]=='}')
                        cout<<"loss of left character {."<<'\n';
                    if(str[i]==')')
                        cout<<"loss of left character (."<<'\n';
                    if(str[i]==']')
                        cout<<"loss of left character [."<<'\n';
                    if(str[i]=='>')
                        cout<<"loss of left character <."<<'\n';
                    break;
                }
            }
        }
    }
    if(flag==1)
    {
        int vv=0;
        if(!st.empty())
        {
            cout<<"loss of right character ";
            vv=1;
        }
        while(!st.empty())
        {
            if(st.top()=='{')
                cout<<"}";
            if(st.top()=='(')
                cout<<")";
            if(st.top()=='[')
                cout<<"]";
            if(st.top()=='<')
                cout<<">";
            st.pop();
        }
        if(vv==1)
            cout<<".";
        return 0;
    }

    if(st.empty())
        cout<<"right.";
    else
    {
        cout<<"Matching failure."<<'\n';
        cout<<"loss of right character ";
        while(!st.empty())
        {
            if(st.top()=='{')
                cout<<"}";
            if(st.top()=='(')
                cout<<")";
            if(st.top()=='[')
                cout<<"]";
            if(st.top()=='<')
                cout<<">";
            st.pop();
        }
        cout<<".";
    }
    return 0;
}

你可能感兴趣的:(数据结构课程,STL)