AtCoder Beginner Contest 157 C.Guess The Number

AtCoder Beginner Contest 157 C.Guess The Number

题目链接

Problem Statement

If there is an integer not less than 0 satisfying the following conditions, print the smallest such integer; otherwise, print -1.

  • The integer has exactly N digits in base ten. (We assume 0 to be a 1-digit integer.
  • For other integers, leading zeros are not allowed.)The si-th digit from the left is ci. (i=1,2,⋯,M)

Constraints

All values in input are integers.

  • 1 ≤ N ≤ 3 1≤N≤3 1N3
  • 0 ≤ M ≤ 5 0≤M≤5 0M5
  • 1 ≤ s i ≤ N 1≤s_i≤N 1siN
  • 0 ≤ c i ≤ 9 0≤c_i≤9 0ci9

Input

Input is given from Standard Input in the following format:

N   M N M N M
s 1   c 1 s_1 c_1 s1 c1

s M   c M s_M c_M sM cM

Output

Print the answer.

Sample Input 1

3 3
1 7
3 2
1 7

Sample Output 1

702
702

Sample Input 2

3 2
2 1
2 3

Sample Output 2

-1

Sample Input 3

3 1
1 0

Sample Output 3

-1

自认为是最坑的一次ABC了,因为和cf冲突,做得比较急,还wa了三发,我把坑点罗列一下,供各位参考:
1. n = 1 , m = 0 n=1,m=0 n=1,m=0 时,输出 0 0 0
2.当 s i = 1 , c i = 0 s_i=1,c_i=0 si=1,ci=0 时,存在前导 0 0 0 ,输出 − 1 -1 1
3.当对一个位置赋不同值时,输出 − 1 -1 1
4.最难想的,当 n > 1 n>1 n>1 时,第一个位置未赋值,比如:

3 1
2 0

应该输出100,也即首位未赋值时要补1
5.未赋值的位置补0或1
就不在代码里挂注释了,AC代码如下:

#include
using namespace std;
typedef long long ll;

int main(){
    int n,m,a[10],s,c;
    map<int,int>q;
    cin>>n>>m;
    int flag=1;
    memset(a,-1,sizeof(a));
    while(m--){
        cin>>s>>c;
        if(q.count(s)==0){
            q[s]=c;
            a[s]=c;
        }
        else{
            if(q[s]!=c) flag=0;
        }
    }
    if(a[1]==0 && n>1) flag=0;
    if(flag==0) puts("-1");
    else{
        if(n>1){
            for(int i=1;i<=n;i++){
                if(a[i]==-1 && i==1) cout<<1;
                else if(a[i]==-1 && i>1) cout<<0;
                else cout<<a[i];
            }
        }
        else{
            if(a[1]==-1) cout<<0;
            else cout<<a[1];
        }
    }
    return 0;
}

你可能感兴趣的:(模拟,AtCoder)