STl的一些具体的例题的应用

来源:互联网 发布:java编程思想 新手 编辑:程序博客网 时间:2024/06/05 14:32

HDU 1004

Let the Balloon Rise

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 115929    Accepted Submission(s): 45434


Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.

This year, they decide to leave this lovely job to you.
 

Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.

A test case with N = 0 terminates the input and this test case is not to be processed.
 

Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
 

Sample Input
5greenredblueredred3pinkorangepink0
 

Sample Output
redpink


该题需要一一对应,因此用的是map

已经AC过的代码:

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
    int n;
    string a;
    while(cin>>n&&n>0)
    {
        map<string, int>ss;
        while(n--)
        {
            cin>>a;
            ss[a]++;
        }
        int max=0;
        string ans;
        map<string, int>::iterator it;
        for(it=ss.begin();it!=ss.end();it++)
        {
            if(it->second>max)
            {
                max=(*it).second;
                ans=(*it).first;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}



sdnuoj

1174.明明的随机数

Time Limit: 1000 MS    Memory Limit: 32768 KB
Total Submission(s): 138    Accepted Submission(s): 56

Description

明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。

Input

输入有2行,第1行为1个正整数,表示所生成的随机数的个数N。
第2行有N个用空格隔开的正整数,为所产生的随机数。

Output

输出也是2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。

Sample Input

1020 40 32 67 40 20 89 300 400 15

Sample Output

815 20 32 40 67 89 300 400

set对于已经出现的数不会再重新记录,而且set对于数据具有自动排序的功能

已经AC过的代码:

#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int main()
{
    set<int>s;
    s.clear();
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        int m;
        scanf("%d",&m);
        s.insert(m);
    }
    int sum;
    sum=s.size();
    printf("%d\n",sum);
    set<int >::iterator it1,it2;
    it1=--s.end();
    for(it2=s.begin();it2!=--s.end();it2++)
        printf("%d ",*it2);
    printf("%d\n",*it1);
    return 0;
}



HDU1276

士兵队列训练问题

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7702    Accepted Submission(s): 3524


Problem Description
某部队进行新兵队列训练,将新兵从一开始按顺序依次编号,并排成一行横队,训练的规则如下:从头开始一至二报数,凡报到二的出列,剩下的向小序号方向靠拢,再从头开始进行一至三报数,凡报到三的出列,剩下的向小序号方向靠拢,继续从头开始进行一至二报数。。。,以后从头开始轮流进行一至二报数、一至三报数直到剩下的人数不超过三人为止。
 

Input
本题有多个测试数据组,第一行为组数N,接着为N行新兵人数,新兵人数不超过5000。
 

Output
共有N行,分别对应输入的新兵人数,每行输出剩下的新兵最初的编号,编号之间有一个空格。
 

Sample Input
22040
 

Sample Output
1 7 191 19 37

已经AC过的代码:

方法一:用普通的数组:

#include<cstdio>
#include<cstring>
int main()
{
    int t,n,k,i;
    int a[5005],b[5005];
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            scanf("%d",&n);
            for(int i=1; i<=n; i++)
                a[i]=i;
            while(1)
            {
                if(n<=3)
                {
                    for( i=1; i<n; i++)
                        printf("%d ",a[i]);
                    printf("%d\n",a[i]);
                    break;
                }
                k=0;
                for(int i=1; i<=n; i++)
                {
                    if(i%2!=0)
                        b[++k]=a[i];
                }
                if(k<=3)
                {
                    for(int i=1; i<k; i++)
                        printf("%d ",b[i]);
                    printf("%d\n",b[k]);
                    break;
                }
                n=0;
                for(int i=1; i<=k; i++)
                {
                    if(i%3!=0)
                        a[++n]=b[i];
                }
            }
        }
    }
    return 0;
}


方法二:用队列完成的:

#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
queue <int> s;
void cmp(int k)
{
    int i = 1;
    while(s.front() != 0)
    {
        if(i % k != 0)    //用取模来实现数数的循环来数到相应的数字
            s.push(s.front());  //把留下来的方队尾
        s.pop();              //去掉队首
        ++i;
    }
    s.pop();               //把队首的0去掉,在队尾加上0
    s.push(0);
}
int main()
{
    int t, n;
    cin >> t;
    while(t-- && cin >>n)
    {
        for(int i = 1; i <=n; i++)
            s.push(i);
        s.push(0);     //在最后面加个0代表队尾的标志
        int i = 1;
        while(s.size() > 4)   //因为加上了0,所以长度小于等于4就退出
        {
            if(i % 2 == 0)     //选择数三下踢一个人还是数两下踢一个人
                cmp(3);        //i是偶数数则cmp(3),否则cmp(2)
            else
                cmp(2);
            i++;
        }
        while(!s.empty())
        {
            if(s.front() != 0)
            {
                cout << s.front();
                s.pop();
                if(s.front() != 0)
                    cout << " ";
            }
            else
                s.pop();//因为队尾还有个0,这个是不用输出的但是为了清空队列,还是要pop他
        }
        cout << endl;
    }
    return 0;
}

方法三:使用链表的方式:

#include<iostream>
#include<list>
using namespace std;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        cin>>n;
        list<int> s;
        if(n==0) cout<<0<<endl;
        else
        {
            for(int i=1; i<=n; i++)
                s.push_back(i);
            list<int>::iterator it,ip;//链表遍历迭代器
            int f=1;
            int flag=s.size();    
            while(flag>3)
            {
                for(it=s.begin(); it!=s.end();)
                {
                    if(f==2)
                    {
                        f=1;
                        ip=it;
                        it++;
                        s.erase(ip);   //抹除操作
                        flag--;
                    }
                    else
                    {
                        f++;
                        it++;
                    }
                }
                f=1;
                if(flag<=3) break;
                for(it=s.begin(); it!=s.end();)
                {
                    if(f==3)
                    {
                        f=1;
                        ip=it;
                        it++;
                        s.erase(ip);
                        flag--;
                    }
                    else
                    {
                        f++;
                        it++;
                    }
                }
                f=1;
                if(flag<=3) break;
            }
            int w=0;
            for(it=s.begin(); it!=s.end(); it++)
            {
                cout<<(*it);              //注意输出格式
                w++;
                if(w<=flag-1) cout<<' ';
            }
            cout<<endl;
        }
    }
    return 0;
}


HDU 5831

Rikka with Parenthesis II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1408    Accepted Submission(s): 631


Problem Description
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".

Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different positioni,j and swap S_i,S_j.

Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.

It is too difficult for Rikka. Can you help her?
 

Input
The first line contains a number t(1<=t<=1000), the number of the testcases. And there are no more then 10 testcases with n>100

For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
 

Output
For each testcase, print "Yes" or "No" in a line.
 

Sample Input
34())(4()()6)))(((
 

Sample Output
YesYesNo

这道题使用stack的方法更方便一点

已经AC过的代码:

#include<cstdio>

#include<iostream>

#include<cstring>

#include<stack>

using namespace std;

int main()

{

    int T,n;

    char a[100010];

    stack<char>s;

    scanf("%d",&T);

    while(T--)

    {

        int l=0,r=0,ans=0,wa=0,flag=1;

        while(!s.empty())

            s.pop();

        scanf("%d",&n);

        if(n)

            scanf("%s",a);

        if(n%2==1) printf("No\n");

        else

        {

            int len=strlen(a);

            for(int i=0; i<len; i++)

            {

                if(a[i]=='(')

                {

                    l++;

                    s.push(a[i]);

                }

                else

                {

                    r++;

                    if(!s.empty()&&s.top()=='(')

                    {

                        ans++;

                        s.pop();

                    }

                    else

                    {

                        wa++;

                    }

                }

            }

            if(l==r)

            {

                if(s.empty())

                {

                    if(ans!=1)printf("Yes\n");

                    else      printf("No\n");

                }

                else if(wa==1)

                    printf("Yes\n");

                else if(wa==2&&!s.empty())

                    printf("Yes\n");

                else

                    printf("No\n");

            }

            else printf("No\n");

        }

    }

    return 0;

}


0 0
原创粉丝点击