2021.3.5

{A} + {B}

题目

Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input
1 2
1
2 3
1 2
1
1 2

Sample Output
1 2 3
1 2

Author
xhd

Source
HDU 2006-5 Programming Contest

题解

合并集合,set比较好用

#include <iostream>
#include <set>

using namespace std;

int main()
{
    int n, m, t;
    while (cin >> n >> m)
    {
        set<int> se;
        for (int i = 1; i <= n+m; i++)
        {
            cin >> t;
            se.insert(t);
        }
        for (set<int>::iterator it = se.begin(); it != se.end(); it++)
        {
            if (it != se.begin())
            {
                cout << " ";
            }
            cout << *it;
        }
        cout << endl;
    }
    return 0;
}

火车进站问题

题目

Problem Description

假设杭州东火车站只有一条铁路,并且所有火车都从一侧进来,从另一侧出去。那么,如果火车A先进站,然后火车B在火车A离开之前就进站,那么火车A直到火车B离开后才能离开,可参见下图。

在这里插入图片描述在这里插入图片描述在这里插入图片描述

现在,假设车站中有n(n<=9)列火车,所有火车都有一个ID(从1到n的编号),火车以O1的顺序进站,您的任务是确定火车是否可以按O2顺序出站。

Input

输入包含几个测试用例。

每个测试用例均包含三部分:一个表示火车数量的整数和两个字符串O1和O2,其中,火车的进站顺序用O1串表示,火车的出站顺序用O2串表示。

输入在文件末尾终止,更多信息参见样例。

Output

如果不能从O1的入站顺序得到O2的出站顺序,请输出字符串“ No.”。

如果能够得到,则请输出"Yes."
然后输出进站和出站的具体方式(“in”表示火车进站,“out”表示火车出站)。
在每个测试用例之后输出一行“ FINISH”。

更多信息参见样例。

Sample Input

3 123 321
3 123 312

Sample Output

Yes.
in
in
in
out
out
out
FINISH
No.
FINISH

题解

典型的stack

#include <iostream>
#include <stack>

using namespace std;

int main()
{
    string s1, s2;
    int n, a[30];
    while (cin >> n)
    {
        cin >> s1 >> s2;
        if (s1.size() !=n || s2.size() != n)
        {
            cout << "No." << endl;
            continue;
        }
        int cnt = 0;
        size_t j = 0;
        stack<char> st;
        st.push('#');
        for (size_t i = 0; i < s1.size() && j < s2.size(); i++)
        {
            st.push(s1[i]);
            a[++cnt] = 1;
            while (st.top() == s2[j] && j < s2.size())
            {
                st.pop();
                a[++cnt] = 0;
                j++;
            }
        }
        if (j != s2.size() || st.size() != 1)
        {
            cout << "No." << endl;
        }
        else
        {
            cout << "Yes." << endl;
            for (int i = 1; i <= n*2; i++)
            {
                if (a[i])
                {
                    cout << "in" << endl;
                }
                else
                {
                    cout << "out" << endl;
                }
            }
        }
        cout << "FINISH" << endl;
    }
    return 0;
}

统计书名

题目

Problem Description

嗷嗷嗷非常喜欢看书。每当他看完一本书,他就会用他的小本本记下书名。但嗷嗷嗷看书时太过忘我,以至于自己看过的书都会再看一遍并照样记录下来。当嗷嗷嗷回过神,想统计自己一共看了多少本不同的书,他把小本本交给了你,你能帮帮他吗?

Input

多组输入输出,请处理到输入结束。
每组数据,第一行有两个整数n (0<n<=100)代表书名的个数。
接下来有n行,每有一个个字符串s,代表书名(仅含大小写字母)

Output

每组数据输出一行,一个整数cnt,代表嗷嗷嗷看了不同的书的数量

Sample Input

5
aoaoao
aoao
acm
meow
aoaoao

Sample Output

4

题解

用map特别简单

#include <iostream>
#include <map>

using namespace std;

int main()
{
    int n;
    string s;
    while (cin >> n)
    {
        set<string> se;
        while (n--)
        {
            cin >> s;
            se.insert(s);
        }
        cout << se.size() << endl;
    }
    return 0;
}

第M个数列

题目

Problem Description

给定1到N的序列,我们定义1,2,3 …N-1,N是由1到N组成的所有序列中的最小序列(每个数字只能使用一次)。则很容易看出,第二个最小的序列是1,2,3 … N,N-1。
现在,给定两个数字N和M,请告诉由数字1到N组成的第M个最小的序列是什么。

Input

输入包含多组测试数据,请处理到文件结束。
每组测试数据包含两个整数N和M(1<=N<=1000, 1<=M<=10000)。
数据保证一定有满足要求的数列。

Output

请输出满足要求的数列。数列的两个数之间用1个空格隔开,并且最后一个数后面没有空格。
每组数据输出一行。

Sample Input

6 4
11 8

Sample Output

1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10

题解

next_permutation 比较容易解

#include <iostream>
#include <algorithm>

using namespace std;

void init(int *a, int n)
{
    for (int i = 0; i < n; i++)
    {
        a[i] = i+1;
    }
}

int main()
{
    int a[1005];
    int N, M;
    while (cin >> N >> M)
    {
        int i = 0;
        init(a, N);
        
        do
        {
            i++;
        }while (i < M && next_permutation(a, a+N));

        for (int j = 0; j < N; j++)
        {
            if (j)
            {
                cout << " ";
            }
            cout << a[j];
        }
        cout << endl;
    }
    return 0;
}

What Are You Talking About

题目

Problem Description

Ignatius is so lucky that he met a Martian yesterday. But he didn’t know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?

Input

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string “START”, this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian’s language. A line with a single string “END” indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string “START”, this string should be ignored, then an article written in Martian’s language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can’t find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(’ ‘), tab(’\t’), enter(’\n’) and all the punctuation should not be translated. A line with a single string “END” indicates the end of the book part, and that’s also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.

Output

In this problem, you have to output the translation of the history book.

Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i’m fiwo riwosf.
i fiiwj fnnvk!
END

Sample Output

hello, i’m from mars.
i like earth!

Hint

Huge input, scanf is recommended.

Author

Ignatius.L

题目大意:翻译火星文

题解

后来重新写了字典树的代码,但是花销太大了,就不贴了

#include <iostream>
#include <map>

using namespace std;

int main()
{
    map<string, string> Map;
    string Martian, s;
    cin >> s;
    if (s == "START")
    {
        while (cin >> s && s != "END")
        {
            cin >> Martian;
            Map[Martian] = s;
        }
    }
    cin >> s;
    if (s == "START")
    {
        getline(cin, s);
        while (getline(cin, s) && s != "END")
        {
            Martian = "";
            for (size_t i = 0; i < s.size(); i++)
            {
                if (s[i] <= 'z' && s[i] >= 'a')
                {
                    Martian += s[i];
                }
                else
                {
                    if (Map.count(Martian))
                    {
                        cout << Map[Martian];
                    }
                    else
                    {
                        cout << Martian;
                    }
                    cout << s[i];
                    Martian = "";
                }
            }
            cout << endl;
        }
    }
    return 0;
}

Seinfeld

题目

Problem Description

I’m out of stories. For years I’ve been writing stories, some rather silly, just to make simple problems look difficult and complex problems look easy. But, alas, not for this one.
You’re given a non empty string made in its entirety from opening and closing braces. Your task is to find the minimum number of “operations” needed to make the string stable. The definition for being stable is as follows:

  1. An empty string is stable.
  2. If S is stable, then {S} is also stable.
  3. If S and T are both stable, then ST (the concatenation of the two) is also stable.
    All of these strings are stable: {}, {}{}, and {{}{}}; But none of these: }{, {{}{, nor {}{.
    The only operation allowed on the string is to replace an opening brace with a closing brace, or visa-versa.

Input

Your program will be tested on one or more data sets. Each data set is described on a single line. The line is a non-empty string of opening and closing braces and nothing else. No string has more than 2000 braces. All sequences are of even length.
The last line of the input is made of one or more ’-’ (minus signs.)

Output

For each test case, print the following line:
k. N
Where k is the test case number (starting at one,) and N is the minimum number of operations needed to convert the given string into a balanced one.
Note: There is a blank space before N.

Sample Input

}{
{}{}{}
{{{}

Sample Output

  1. 2
  2. 0
  3. 1

(上面是序号加点加空格然后输出结果)

Source

2009 ANARC

题解

#include <iostream>
#include <stack>

using namespace std;

int main06()
{
    int k = 1;
    string s;
    while (getline(cin, s) && s[0] != '-')
    {
        stack<char> st;
        st.push('#');
        int cnt = 0;
        for (size_t i = 0; i < s.size(); i++)
        {
            if (s[i] == '}')
            {
                if (st.top() == '{')
                {
                    st.pop();
                }
                else
                {
                    st.push('{'); // 如果栈顶不是左括号,则当前的右括号需要改变。
                    cnt++;
                }
            }
            else if (s[i] == '{')
            {
                st.push(s[i]);
            }
        }
        // (st.size()-1)是剩的左括号,变一半就行
        cout << k++ << ". "<< cnt + (st.size()-1)/2 << endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值