registration system(map+思维)

     题意:每次给出一个只含小写字母的字符串,如果之前没出现过,就输出YES,插入到数据库,否则输出  它+数字   的形式,数字按出现顺序排列。

     解析:比如只输入a,会陆续出现a,a1,a2,a3......a10,a11。可以看出来,我们压根不需要每次判断当前字符串的上次出现是什么样子,其实本质上就是原字符串+出现次数的形式而已。所以用map记录次数,每次都++记录下就可以了。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<string>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
string s;
int a[maxn];
int main()
{
    int n;
    cin>>n;
    map<string,int>mp;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        if(mp[s]==0)
        {
            cout<<"OK"<<endl;
            mp[s]++;
        }
        else
        {
            cout<<s<<mp[s]<<endl;
            mp[s]++;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/liyexin/p/12757311.html