STl的一些具體的例題的應用

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
5 green red blue red red 3 pink orange pink 0
 

Sample Output
red pink


該題需要一一對應,因此用的是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

10
20 40 32 67 40 20 89 300 400 15

Sample Output

8
15 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
2 20 40
 

Sample Output
1 7 19 1 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 position i,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
3 4 ())( 4 ()() 6 )))(((
 

Sample Output
Yes Yes No

這道題使用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;

}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章