[Luo Gu P1013] NOIP1998 improve group hex digits

Problem Description

Luce famous scientist in order to check students' understanding of the binary system, he gives the following addition of a table, the letter represents the numbers in the table. E.g:

LKVE
LLKVE
KKVE KL
VVE KL KK
EE KL KK KV

Its meaning is:

L+L=L,L+K=K,L+V=V,L+E=E

K+L=K,K+K=V,K+V=E,K+E=KL

...... E + E = KV

These rules can be derived: L = 0, K = 1, V = 2, E = 3, the table may be determined at the same time it represents the binary adder 4

Input Format

n (n≤9) represents the number of rows.

The following n rows, each row comprising n strings, each string separated by spaces. (A string of only '+' sign, are the capital letters)

Output Format

① what each letter represents a number of formats such as: L = 0, K = 1, ...... alphabetically by analysis.

② adding a few decimal.

③ If the table is not possible adder composition should output "ERROR!"

Sample input

5
LKVE
LLKVE
KKVE KL
VVE KL KK
EE KL KK KV

Sample Output

L=0 K=1 V=2 E=3
4

Resolve

When you see this question, there is no thought of a search? A pass code is then ...... then before.

However, search really use it?

We can look at. For the n-ary number \ (I \) , if the \ (I \) plus some number \ (J \) becomes two digits, you can obtain the following inequality:
\ [I + J> N- 1 \ Rightarrow j> n-1
-i \] satisfies the requirements \ (J \) number with a \ (n-1- (n- 1-i) = i \) a. From this we can conclude that a value is the letter corresponding to the letter row of the two-digit number. We need to do is verify correct. So how do you verify it? The most direct way is to directly fill generations, but can use another method to calculate the value of each letter in it?

The more difficult want. For a number \ (I \) , if you want to \ (j + k \) th digit of \ (I \) , must satisfy \ (I <K <n-\) . Suppose, then, that satisfy the condition \ (K \) has \ (a [i] \) a, \ (I \) value is \ (. 1-n--A [I] \) . \ (a [i] \) to request only the number of occurrences of the letter on a double-digit bits.

In addition, if a number appears twice on the same line, obviously it is not right, directly to end.

In the following code, since the number of lines is \ (n-\) , so they are actually \ (n-1 \) binary addition.

Code

#include <iostream>
#include <cstdio>
#include <string>
#define N 10
using namespace std;
int n,i,j,a[N],num[30];
char l[N];
int main()
{
    cin>>n;
    for(i=0;i<n;i++){
        char c;
        cin>>c;
        if(c!='+') l[i]=c;
    }
    for(i=2;i<=n;i++){
        string s1,s;
        for(j=1;j<=n;j++){
            cin>>s;
            if(j==1) continue;
            if(j!=2&&s==s1){
                cout<<"ERROR!"<<endl;
                return 0;
            }
            s1=s;
            if(i!=1&&j!=-1){
                int l=s.length();
                if(l==2){
                    a[i-1]++;
                    num[(int)s[l-1]]++;
                }
            }
        }
    }
    for(i=1;i<n;i++){
        if(a[i]!=n-2-num[(int)l[i]]){
            cout<<"ERROR!"<<endl;
            return 0;
        }
    }
    for(i=1;i<n;i++){
        cout<<l[i]<<"="<<a[i]<<' ';
    }
    cout<<endl<<n-1<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/LSlzf/p/11129724.html