HDU 4167 User Names

User Names

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 483    Accepted Submission(s): 167


Problem Description
A university's computer system assigns user names according to the following set of rules:

The maximum length of a username is MAXLEN characters. (The value of MAXLEN will be specified in the input for each problem instance.)
The first character of the user name is the first letter of the person's first name, converted to lower case. Ignore apostrophes and hyphens here and in Step 3.
Append as many letters of the person's last name as possible (converted to lower case, if necessary), without exceeding a total of MAXLEN characters. Starting with the first letter of the last name, append these letters in the order in whch they appear in the last name.
If a user name assigned on basis of Rules 1 - 3 already exists in the database, break the tie as follows: append serial numbers 1 - 9, in that order, to the username from step 3, if that can be done without exceeding the limit of MAXLEN characters in the username. Otherwise, drop the last letter before appending the serial number.
If a user name assigned on basis of Rules 1 - 4 already exists in the database, break the tie as follows: append serial numbers 10 - 99, in that order, to the username from step 3, if that can be done without exceeding the limit of MAXLEN characters in the username. Otherwise, drop the last letter or the last two letters (whichever is necessary) before appending the serial number.
It is assumed that the above rules will avoid ties.
 

Input
The input will contain data for a number of test cases. The first line of each test case will contain two positive integers: the number of names and the value of MAXLEN (5 <= MAXLEN <= 80). This will be followed by the list of names. Each name will consist of at most 80 characters and will begin with the first name, followed by middle names, if any, and will conclude with the last name. A single blank space will separate first, middle, and last names. Any name can contain upper and lower case letters, hyphens, and apostrophes. A last name will contain at least two letters, other names will contain at least one letter (they could be just initials). There will be no more than 200 names in each case. The last test case will be followed by a line containing two zeros for the number of names and MAXLEN.
 

Output
For each case, the output will begin with a line containing the case number. This will be followed by the list of user names, one per line, in the same order as the corresponding names in the input.
 

Sample Input
  
  
2 6 Jenny Ax Christos H Papadimitriou 11 8 Jean-Marie d'Arboux Jean-Marie A d'Arboux Jean-Marie B d'Arboux Jean-Marie C d'Arboux Jean-Marie D d'Arboux Jean-Marie D d'Arboux Jean-Marie F d'Arboux Jean-Marie G d'Arboux Jean-Marie H d'Arboux Jean-Marie I d'Arboux Jean-Marie J d'Arboux 11 9 Jean-Marie d'Arboux Jean-Marie A d'Arboux Jean-Marie B d'Arboux Jean-Marie C d'Arboux Jean-Marie D d'Arboux Jean-Marie D d'Arboux Jean-Marie F d'Arboux Jean-Marie G d'Arboux Jean-Marie H d'Arboux Jean-Marie I d'Arboux Jean-Marie J d'Arboux 0 0
 

Sample Output
  
  
Case 1 jax cpapad Case 2 jdarboux jdarbou1 jdarbou2 jdarbou3 jdarbou4 jdarbou5 jdarbou6 jdarbou7 jdarbou8 jdarbou9 jdarbo10 Case 3 jdarboux jdarboux1 jdarboux2 jdarboux3 jdarboux4 jdarboux5 jdarboux6 jdarboux7 jdarboux8 jdarboux9 jdarbou10
 

Source
 

Recommend
lcy



代码不是重点  重点是下面的图

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<vector>
#include<map>
using namespace std;

map<string,int>ans;
int main ()
{
	int n, m;
	int i, j;
	int t = 1;
	while(scanf("%d %d", &n, &m), n||m )
	{
		 ans.clear();
		printf("Case %d\n",t++);
		char s[205],user[205];
		getchar();                     //这个地方是个天坑啊  请看下面图解
        //getchar();

		for(int cnt = 0; cnt < n; cnt++)
		{

			gets(s);
			//printf("*****%s\n",s);
			int len = strlen(s);

			if(s[0] < 'a')  user[0] = s[0] + 32;
			else            user[0] = s[0];

			for(i = len - 1; i >= 0; i--)
				if(s[i] == ' ')
					break;


			for(i = i+1 , j = 1; i < len && j < m; )
			{
				 if(s[i] >= 'a' && s[i] <= 'z')
				 {
					  user[j] = s[i];
					  i++; j++;
				 }
				 else if(s[i] >= 'A' && s[i] <= 'Z')
				 {
					 user[j] = s[i] + 32;
					 i++; j++;
				 }
				 else
					 i++;
			}
			user[j] = '\0';

			map<string,int>::iterator iter = ans.find(user);
			 if(iter == ans.end())
			 {
				 ans.insert( make_pair <string,int> (string(user),1));
				 printf("%s\n",user);
			 }
			 else
			 {
				 iter->second++;
				 int len = strlen(user);
				 if(len < m)
				 {
                     int temp = iter->second - 1;
				     int l = 0;
				     while(temp > 0)
				     {
				         temp = temp /10;
				         l++;
				     }
				     if(l > m-len){
				       for(i = 0; i < m-l; i++)
						   printf("%c",user[i]);
						   }
                    else
                        printf("%s",user);

				      printf("%d\n",iter->second - 1);
				 }

				 else
				 {
				     int temp = iter->second - 1;
				     int l = 0;
				     while(temp > 0)
				     {
				         temp = temp /10;
				         l++;
				     }
					 for(i = 0; i < m-l; i++)
						 printf("%c",user[i]);
					 printf("%d\n",iter->second - 1);
				 }
			 }

		}
	}
	return 0;
}



两个getchar()的  WA  了
而只有一个getchar()的 竟然AC了 !!!!
跪求大神解释解释啊~~~






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值