Peter wrote on the board a strictly increasing sequence of positive integers a1, a2, ..., an. Then Vasil replaced some digits in the numbers of this sequence by question marks. Thus, each question mark corresponds to exactly one lost digit.
Restore the the original sequence knowing digits remaining on the board.
The first line of the input contains integer n (1 ≤ n ≤ 105) — the length of the sequence. Next n lines contain one element of the sequence each. Each element consists only of digits and question marks. No element starts from digit 0. Each element has length from 1 to 8 characters, inclusive.
If the answer exists, print in the first line "YES" (without the quotes). Next n lines must contain the sequence of positive integers — a possible variant of Peter's sequence. The found sequence must be strictly increasing, it must be transformed from the given one by replacing each question mark by a single digit. All numbers on the resulting sequence must be written without leading zeroes. If there are multiple solutions, print any of them.
If there is no answer, print a single line "NO" (without the quotes).
3 ? 18 1?
YES 1 18 19
2 ?? ?
NO
5 12224 12??5 12226 ?0000 ?00000
YES 12224 12225 12226 20000 100000
题意:给你n个字符串代表n个数,每个字符串中可能包含?,?可以代替为任何数字,让你求如何使n个数字为严格递增数列。
做法:首先第一个先进行操作,遇到?判断是否是第一位如果是第一位则赋值为1,其他则赋值为0。然后2-n之间进行操作,用l1记录前一个长度,l2记录当前长度,如果l1>l2则直接输出NO结束,如果l1<l2则像第一个字符那样操作,主要麻烦的是l1==l2的情况,首先从首部搜过去,type先记为3,如果搜到当前数字比上一个字符的数字小,标记为type1,并记录当前位置。type1中从记录位置从前搜,搜到当前数字为?并且前一字符的数字不为9。如果当前数字比上一个字符的数字大,则标记为type2,type2中直接进行复制,记录位置前?赋值为前一字符串数字,记录位置后则赋值为0.如果两种情况都不是则是type3,type3从末尾往前搜搜到当前数字为?并且前一字符的数字不为9并记录当前位置。然后进行赋值就行。
模拟过程有点繁琐。
#include <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL long long
#define inf 0x0f0f0f0f
using namespace std;
char s[100005][10];
int main()
{
int n;
int i,j,k;
int flag;
while(scanf("%d",&n)!=EOF)
{
flag=1;
for(i=1;i<=n;i++)
scanf("%s",s[i]);
for(j=0;j<strlen(s[1]);j++)
{
if(s[1][j]=='?'&&j==0)
s[1][j]='1';
if(s[1][j]=='?'&&j!=0)
s[1][j]='0';
}
for(i=2;i<=n;i++)
{
int l1,l2,pos,flag1;
l1=strlen(s[i-1]);
l2=strlen(s[i]);
flag1=3;
if(l1==l2)
{
for(j=0;j<l1;j++)
{
if(s[i][j]!='?')
{
if(s[i][j]>s[i-1][j])
{
flag1=2;
pos=j;
break;
}
if(s[i][j]<s[i-1][j])
{
flag1=1;
pos=j;
break;
}
}
}
if(flag1==2)
{
for(j=0;j<pos;j++)
if(s[i][j]=='?')
s[i][j]=s[i-1][j];
for(j=pos+1;j<l1;j++)
if(s[i][j]=='?')
s[i][j]='0';
}
if(flag1==1)
{
int a=0;
for(j=pos-1;j>=0;j--)
{
if(s[i][j]=='?'&&s[i-1][j]!='9')
{
s[i][j]=s[i-1][j]+1;
a=1;
break;
}
}
if(a==0)
{
flag=0;
goto loop;
}
else
{
for(k=0;k<j;k++)
if(s[i][k]=='?')
s[i][k]=s[i-1][k];
for(k=j+1;k<l1;k++)
if(s[i][k]=='?')
s[i][k]='0';
}
}
if(flag1==3)
{
int b=0;
for(j=l1-1;j>=0;j--)
if(s[i][j]=='?'&&s[i-1][j]!='9')
{
s[i][j]=s[i-1][j]+1;
b=1;
break;
}
if(b==0)
{
flag=0;
goto loop;
}
else
{
for(k=0;k<j;k++)
if(s[i][k]=='?')
s[i][k]=s[i-1][k];
for(k=j+1;k<l1;k++)
if(s[i][k]=='?')
s[i][k]='0';
}
}
}
if(l1>l2)
{
flag=0;
goto loop;
}
if(l2>l1)
{
for(j=0;j<l2;j++)
{
if(s[i][j]=='?'&&j==0)
s[i][j]='1';
if(s[i][j]=='?'&&j!=0)
s[i][j]='0';
}
}
}
loop:
if(flag==0)
printf("NO\n");
else
{
printf("YES\n");
for(i=1;i<=n;i++)
printf("%s\n",s[i]);
}
}
}