您的位置:首页 > 其它

Codeforces Round #347 (Div. 2) C. International Olympiad(求第一次出现这种数字结尾的是哪一个年份)

2016-04-25 19:44 633 查看
C. International Olympiad

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands
for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used
before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.

For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9,IAO'0 and IAO'1,
while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used
in 1995.

You are given a list of abbreviations. For each of them determine the year it stands for.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 1000) —
the number of abbreviations to process.

Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine
digits.

Output

For each abbreviation given in the input, find the year of the corresponding Olympiad.

Examples

input
5
IAO'15
IAO'2015
IAO'1
IAO'9
IAO'0


output
2015
12015
1991
1989
1990


input
4
IAO'9
IAO'99
IAO'999
IAO'9999


output
1989
1999
2999
9999


题意:

从1989年起算,输出一个n,表示有n组样例,样例形式为IAO’+一串数字,问第一个以这个数字标记结尾的年份是多少。

比如IAO'9, IAO'0 and IAO'1分别表示为1989,1990,1991

 

思路:

由于后面三位考虑比较复杂,我把1989到10000的结尾都预处理出来了。

这个时候如果后面的数字的长度小于等于3,那么我们可以直接输出答案

 

如果后面的数字的长度大于3, 我们先考虑四位数的情况,

那么会有两种可能,

一是他本身,

二是前面加个1

比如3010,我们如果在其前面加了1,那么在他之前以010结尾的数字便有2010,3010,4010,5010,...9010,有8种以010结尾的数字,所以13010要么是以3010结尾,要么是以13010结尾,而不可能以010,10,0结尾

所以假设我们求出了以010结尾的数字是x,如果这个数字x,如果这个数字比3010小,那么以3010结尾的最小的数字一定为3010,否则至少为13010

当位数为5位,6位的时候也是这个道理。

 

方法二:

暴力找规律

当为一位数时,输出的年份为1989-1998

当为二位数时,输出的年份为1999-2098

当为三位数时,输出的年份为2099-3098

当为四位数时,输出的年份为3099-13098

当为五位数时,输出的年份为13099-113098

...

...

#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
map<string,int>mp;

int main(){
for(int i=1989;i<10000;i++){
int num=i;
string s="";
while(num!=0){
string s1="";
char c=num%10+'0';
s1+=c;
s1+=s;
num/=10;
if(mp[s1]==0){
mp[s1]=i;
break;
}
s=s1;
}
}
char s[100];
int n;
scanf("%d",&n);
while(n--){
scanf("%s",s);
string s1="";
for(int i=4;s[i]!='\0';i++)
s1+=s[i];
if(s1.size()<=3||mp[s1]!=0)
printf("%d\n",mp[s1]);
else{
string s2="";
ll P=1,number=0;
for(int i=s1.size()-1;i>=s1.size()-3;i--){
number=(s1[i]-'0')*P+number;
P*=10;
s2+=s1[i];
}
reverse(s2.begin(),s2.end());
ll num=mp[s2];
for(int i=s1.size()-4;i>=0;i--){
if(P*(s1[i]-'0')+number>num)
num=P*(s1[i]-'0')+number,number=P*(s1[i]-'0')+number;
else
num=P*(s1[i]-'0')+number+P*10,number=P*(s1[i]-'0')+number;
P*=10;
}
printf("%lld\n",num);
}
}
}
/*
100
IAO'111112940
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: