NYOJ991Registration system

题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=991

还是STL中的set
头文件#include <set> using namespace std;
声明:set<string> s.
如果来了一个字符串a,用s.count(a)判断s中是否存在a,如果不存在a,直接输出OK并用s.insert(a)把a插入到s中,如果存在了,就从a1一直往后找,直到找到一个在s中没有的字符串,输出并插入到s中,这里用到了一个stringstream ;
解释一下stringstream 这个是用来将int型数据转化为字符串的;
如:
#include <iostream>
#include <string>
#include <sstream>   //stringstream 头文件

using namespace std;

int main()

{
    string s;
    int n;
    cin >> n;
    stringstream ss;
    ss << n;
    ss >> s;
    cout << s << endl;
}

下面是本题的AC码
#include <iostream>
#include <set>
#include <cstdio>
#include <string>
#include <sstream>

using namespace std;

set<string> s;

int main()

{
    int n;
    int cnt;
    cin >> n;
    while(n--)
    {
        string a;
        cin >> a;
        if(!s.count(a))
        {
            cout << "OK" << endl;
            s.insert(a);
        }
        else
        {
            cnt = 1;
            for(int i = cnt;;++i)
            {
                string t;
                stringstream ss;
                ss << i;
                ss >> t;
                if(!s.count(a + t))
                {
                    cout << a + t << endl;
                    s.insert(a + t);
                    break;
                }
            }
        }
    }
    return 0;
}


你可能感兴趣的:(set,ACM,STL,stringstream)