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

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

 

思路:

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

这个时候如果后面的数字的长度小于等于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
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值