T292219 [传智杯 #5 练习赛] 复读

题目描述

给定若干个字符串,不定数量,每行一个。有些字符串可能出现了多次。如果读入一个字符串后,发现这个字符串以前被读入过,则这个字符串被称为前面相同的字符串的复读,这个字符串被称为复读字符串。相应的,每个首次出现的字符串就是非复读字符串

举个例子,

abc
def
abc
abc
abc

第 1,3,4,51,3,4,5 行是字符串 abc,那么 3,4,53,4,5 行的字符串会被称为“复读”。

请你把所有的非复读字符串,按照行号从小到大的顺序,依次拼接为一个长串并输出。

输入格式

多个字符串,每行一个,含义见题目描述。

注意:如果这个字符串是 0,说明所有字符串都读完了。这个 0 不认为是一个“非复读字符串”。

输出格式

共一行,表示所有非复读字符串,按照行号从小到大依次连接的结果。

输入输出样例

输入 #1复制

cc
b
a
cc
0

输出 #1复制

ccba

说明/提示

【数据范围】

字符串的个数不超过 500500 个,字符串总长度不超过 5000050000,每个字符串中只包含小写字母、数字、 . 、! 和 &,不包含空格等特殊符号。

#include <iostream>
#include <algorithm>
#include <climits>
#include <stdio.h>
#include <cstdlib>
#include <memory.h>
#include <queue>
#include <stack>

using namespace std;
string a[510]; //存放字符串
int count1 = 0;

int main()
{
    string s;
    cin >> s;

    while(s != "0")
    {
        bool isRepeat = false;
        for(int i = 0; i < count1; i++)
        {
            if(a[i] == s) //字符串是重复的就跳出
            {
                isRepeat = true;
                break;
            }
        }
        if(isRepeat == false)
        {
            a[count1++] = s;
        }
        else
            isRepeat = false;

        cin >> s;
    }
    for(int i = 0; i < count1; i++)
    {
        cout << a[i];
    }

    return 0;
}



猜你喜欢

转载自blog.csdn.net/weixin_63484669/article/details/127940176