您的位置:首页 > 编程语言 > C语言/C++

CodeForces - 4C

2017-01-25 23:12 781 查看
A new e-mail service "Berlandesk" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration
system. The system should work on the following principle.

Each time a new user wants to register, he sends to the system a request with his
name. If such a
name does not exist in the system database, it is inserted into the database, and the user gets the response
OK, confirming the successful registration. If the
name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and
also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to
name (name1,
name2, ...), among these numbers the least
i is found so that
namei does not yet exist in the database.

Input

The first line contains number n (1 ≤ n ≤ 105). The following
n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

Output

Print n lines, which are system responses to the requests:
OK in case of successful registration, or a prompt with a new name, if the requested name is already taken.

Example

Input
4abacaba

acaba

abacaba

acab
Output
OK

OK

abacaba1

OK
Input
6

first

first

second

second

third

third
Output
OK

first1

OK

second1

OK

third1

这个题提交上去之后失败了跟多次,弄了好几个小时终于发现了原因:题目规定输入的字符串最多有32个字符,我就在规定二位数组大小的时候将长度定义为32,在输入字符串的时候如果正好输入32位字符,那么就没有了字符串结束符'\0'的位置,这样的话在输出字符的时候,输出就会越界,造成输出错误
下面是AC代码
#include<stdio.h>
#include<string.h>
main()
{
long i,n,count=0,jilu[100000]={0},m;
char chucun[50],qian[100000][50];
scanf("%ld",&n);getchar();
while(n--)
{
m=0;
gets(chucun);
for(i=0;i<=count;i++)
if(strcmp(chucun,qian[i])==0)
{jilu[i]++;m=1;break;}
if(m==0){strcpy(qian[count],chucun);count++;printf("OK\n");}
if(m){printf("%s%ld\n",chucun,jilu[i]);}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C语言 oj题 新手