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代码
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]);} } }
相关文章推荐
- codeforces-4C-Registration system
- codeforces 4C Registration system
- CodeForces 4C-Registration system
- Codeforces 4C (STL map)
- codeforces——4C—— Registration System
- (CodeForces - 4C)Registration system(map)
- Codeforces 4C-Registration system(map)
- Codeforces-4C-Registration system
- codeforces-4C-C. Registration system( 自己的算法根本没有效率可言,TLE! )
- Codeforces 4C
- CodeForces 4C
- CodeForces 268A Games
- codeforces 538A
- Codeforces-632D Longest Subsequence
- codeforces-367#B. Interesting drink
- Codeforces 599C Day at the Beach【思维+优先队列】
- Codeforces 849 B. Tell Your World(计算几何)
- Codeforces 782B The Meeting Place Cannot Be Changed(二分)
- Codeforces 28B. pSort 连通性
- Codeforces 501 B - Misha and Changing Handles