acm white novice must-see series (9) - Stack succinctly and examples

acm white novice must-see series (9) - Stack succinctly and examples

Here Insert Picture DescriptionAdvanced stack after the stack
Here Insert Picture DescriptionHere Insert Picture Descriptioncan stack imagine an upper cup opening. . . .
Stack can be defined as insert and delete operations on one end

1. programmer problem
when the programmer input program errors, you can take the following remedial measures: press the wrong key, you can fill a pressing backspace character "#" to show the previous character is invalid ; discover the current line is wrong , you can press a recession symbol "@" to represent the "@" character before a newline between all invalid .
Input
type in one line, the number does not exceed 100.
Output
Output a line of characters, represents the actual effective character.
The Sample the Input
sdfosif @ for (II # =. 1, #; I <# =. 8; I +++ #.);
The Sample the Output
for (I =. 1; I <=. 8; I ++);
Hint
example of the input 2: 1 234 ##
example of an output 2:12

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>//万能头文件
using namespace std;

int main()
{
    stack <char> s1;//定义一个栈
    stack <char> s2;
    char str[110];
    gets(str);//输入
    int len=strlen(str);//长度函数
    for(int i=0; i<len; i++)
    {
        if(str[i]=='@')
        {
            while(!s1.empty())//判断是否为空
            //while(!s1)的意思是当!s1为真时循环,否则跳出循环。
            {
                s1.pop();//出栈将@之前的字符制空
            }
            continue;//全部制空后继续
        }
        if(str[i]=='#')
        {
            if(!s1.empty())
            {
                s1.pop();
                continue;//只制空一个字符
            }
        }
        s1.push(str[i]);//继续入栈
    }
    while(!s1.empty())
    {
        s2.push(s1.top());//将一个栈的元素运送到另一个栈中,假如录入第一个栈顺序为1,2,3
        //则进入另一个栈中的顺序为3,2,1
        //则最后输出顺序就为1,2,3(看上面的图理解一下)
        s1.pop();
    }
    while(!s2.empty())
    {
        printf("%c",s2.top());
        s2.pop();//”倒着"出栈
    }
    printf("\n");
    return 0;
}

2. matching brackets
assumption allows expression contains two kinds of brackets and brackets parentheses, nested random order, such as ([] ()) or [([] [])] as the correct match, [( ]) or ([] () or (())) are matched error.
The title of the task is to test a given expression in parentheses match correctly.
Enter a string containing only parentheses and brackets, it is determined whether the string brackets, the matching outputs "OK", do not match output "Wrong".
Input
line characters, parentheses and brackets containing only the number less than 255.
Output
matching to output a line of text "OK", do not match the output of a line of text "Wrong".
The Input the Sample
[(])
the Sample the Output
Wrong

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[300];
int main()
{
  scanf("%s",s);
    int l=strlen(s);
    stack<char>q;//初始化栈
    char tmp;
    for(int i=0; i<l; i++)
    {
        if(q.empty())
        {
            q.push(s[i]);//入栈
        }
        else
        {
            tmp=q.top();//赋值栈顶
            if((tmp=='['&&s[i]==']')||(tmp=='('&&s[i]==')'))//准确理解题意,
            {
                q.pop();//出栈
            }
            else
            {
                q.push(s[i]);//入栈
            }
        }
    }
    if(q.empty())//栈的输出
    {
        printf("OK\n");
    }
    else
    {
        printf("Wrong\n");
    }
    return 0;
}

3. addition and multiplication of
a given arithmetic expression containing only addition and multiplication, please programmed value calculation expression.
Input
enter only one row, for the need to calculate the expression. 表达式中只包含数字、加法运算符“+”和乘法运算符“*”,且没有括号,所有参与运算的数字均为 0~2^31 -1 之间的整数。输入数据保证这一行只有0~9、+、* 这 12 种字符。
Output
Output a single line containing an integer that represents the value of the expression. Note: When the answer to the length of more than 4, only the last four output request, leading 0 is not output.
Sample Input
[1] SAMPLE INPUT
1 + 1 3 + 4
[1] Output Sample
8
[2] Input Sample
1 +1234567890
1
[2] Sample Output
7891
[3] Input Sample
1 * 1 + 1000000003
[ 3] output sample
4

Sample Output

#include <iostream>
#include <stack>
#include <cstdio>
using namespace std;//此题不讲解,自悟
stack<int>s;
int flag;
void f()//定义一个函数
{
     int xx;
     char chch;
     int tmptmp=s.top();
            s.pop();
            scanf("%d",&xx);
            s.push(((tmptmp%10000)*(xx%10000))%10000);//如果多于4位只输出后四位
            scanf("%c",&chch);
            if(chch=='*') 
            f();          
            if(chch=='\n')
             {flag=1;}
}
int main()
{
    int x,ans=0;
    char ch;
    while(1)
    {
        scanf("%d",&x);
        s.push(x);//入栈
        scanf("%c",&ch);
        if(ch=='+')
         continue;
        if(ch=='*')
        f();
        if(ch=='\n'||flag==1)
        {break;}
    }
    while(!s.empty())
    {
    ans=(s.top()%10000+ans)%10000;
    s.pop();
    }
    cout<<ans<<endl;
    return 0;
}

**

Past Events

**
Original link: dichotomy
1. scrambled ordering
has n (1 <= n <= 2000005) integer, is scrambled, now additionally to an integer x, please find the first one after the ordered sequence is greater than x the number of index!
Input
input data comprising a plurality of test cases, each set of data consists of two lines, the first row is n and x, the second line is already ordered integers n number of columns.
Output
For each test case, find the index sequence from small to large order after the first one is larger than the number of x! .
The Input the Sample
. 3. 3
. 1 2. 4
the Sample the Output
2

#include <iostream>
#include <bits/stdc++.h>//超级简单的一道题
#include <algorithm>
#include <stdio.h>
using namespace std;
const int N=2E6+9;
int a[N];
int main()
{
    int n,x,ans;
    while(~scanf("%d%d",&n,&x))
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);
       int ans=upper_bound(a,a+n,x)-a;
        cout <<ans<< endl;
    }
    return 0;
}

2. segments
for a given length N is a positive integer number of columns Ai, want now divided into M segments (M ≦ N), and each successive requirements, and the maximum and minimum of each segment.
About the smallest maximum value:
for example, a number of columns 42451 to be divided into 3 sections
be segmented as follows:

[4 2][4 5][1]

First and 6, paragraphs 2 and 9, 1, and a maximum of paragraphs 3 and 9.

Segment which is as follows:

[4][2 4][5 1]

For the first stage and 4, paragraph 22 and 6, 6, and 33 and for the maximum segment 6.
And in any case the segment, the maximum value of not less than 6.
To get the number of columns can be 42,451 to be divided into three segments, and a minimum of 6 maximum.
Input
line 11 comprises two positive integers N, M.
Line 22 separated by a space comprising N non-negative integer A_i
as defined topics.
Output
a positive integer, that is how much each segment and maximum minimum.
The Input the Sample
. 5. 3
. 4 2. 4. 5. 1
the Sample the Output
. 6
Hint
N≤100000, M ≦ N, the sum is less than A I 1e9

本题最大值的最小化
注意  l=max(a[i]),r=sum(a[i]),这是必须的,例如:
5 3
1 2 5 888888 4
会发现当mid=3时,8888884 都没起作用,和题意不符;
见代码:
#include <bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
int n,k;
int check(int x)
{
    int num=0,sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[i];
        if (sum>x)
        {
            num++;
            sum=a[i];
        }
    }
    if (num>k) return 1;
    else return 0;
}

int check1(int x)
{
    int num=0,sum=0;
    for(int i=1;i<=n;i++)
    {
        sum+=a[i];
        if (sum>x)
        {
            num++;
            sum=a[i];
        }
    }
    return num;
}
int main()
{
    //ios::sync_with_stdio(false);
    int len=0,mid,ma=0;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
        {
        cin>>a[i];
        len+=a[i];
        ma=max(ma,a[i]);
        }
    int l,r;
    l=ma;r=len;//这里很重要,l必须要等于max(a[i])
    while(l<=r)
    {
        mid=(l+r)/2.0;
        if (check(mid)==1)
            l=mid+1;
        else
            r=mid-1;
    }
    cout<<l<<endl;
    return 0;
}

The current practice

1. Solution preparation
small blue Although there are many solutions, but still no way dubbed want a solution, because if the perversion there is no way to restore. Thus, small blue to the Internet to download a configuration simulator solution. Solution for a virtual simulator configured in a computer, and can be virtually certain concentration added to the virtual current solution, a volume of this solution, the simulator will be calculated quickly poured into a virtual volume after concentration of the solution. Of course, if the perversion can be undone.
Simulator using the following steps:
1) the initial volume and concentration provided a simulator of V0, C0%.
2) a series of operations, the emulator supports two modes of operation:
P (v, c) Action: indicates the current virtual volume was added a solution of concentration c v solution;
the Z operation: Undo the last step of the P operation.
Input
first line two integers, and V0 represents C0,0≤C0≤100;
second row an integer n, operands, n≤10000;
next n lines of an operating format: P_v_c or Z .
Where _ represents a space, only when the time of the initial solution, there is no use and then withdrawn, then only the output of the initial volume and concentration.
Mass at any time will not exceed 2 ^ 31 -1.
The Output
n-lines of two numbers Vi, Ci of, where Vi is an integer, a real number Ci of (five decimal places).
Wherein, the i-th row denotes the volume and concentration of the solution after the i-th operation.
The Input the Sample
100 100
2
P 100 0
the Z
the Sample the Output
50.00000 200
100 100.00000
Hint
example of the input 2:
100 100
2
the Z
P 100 0
Examples 2 Output:
100 100.00000
200 50.00000
next section tips
(10) acm novice must see White Series - queue and examples succinctly

Published 10 original articles · won praise 77 · views 8308

Guess you like

Origin blog.csdn.net/qq_45899321/article/details/103938698