Rokua P1092 Insect Food calculation Label:dfs

Source: Internet
Author: User

Title Description

The so-called insect food calculation, is a part of the original formula has been chewed off by the worm, we need to determine the number of the remaining numbers to be chewed off the letter. Let's look at a simple example:

43#9865#045

+8468#6633

44445509678

The # number represents the number eaten by worms. According to the calculation, we can easily judge: the first row of two numbers are 5 and 3, the second row of the number is 5.

Now, let's do two restrictions on the problem:

First of all, we only consider the addition of insect food calculation. The addition here is N-binary addition, the calculation of three numbers have n bits, allowing a leading 0.

Secondly, the worm eats up all the numbers, we only know which ones are the same, we use the same numbers in the same letters, different numbers are represented by different letters. If this equation is n-ary, we take the first n capitals of the English alphabet to represent the n different numbers from 0 to N-1 in this equation: But the n-letters do not necessarily represent 0 to N-1 in sequence. Input data guarantees that n letters appear at least once.

Badc

    • CBDA

The formula above DCCC is a 4-binary calculation. Obviously, we just have to let ABCD represent 0123, so we can make this equation. Your task is to find the number represented by n different letters for a given n-ary addition calculation, so that the addition formula is set up. Input data guarantees that there is only one set of solutions

Input/output format

Input format:

Contains four rows. The first line has a positive integer N (n<=26), and the following 3 lines each have a string of uppercase letters representing two addend and A and. There are no spaces at either end of the 3 strings, from high to low, and there are exactly n bits.

Output format:

Contains a row. In this line, the unique set of solutions should be included. The solution is that: output n numbers, respectively, a,b,c ... The number represented, the adjacent two digits are separated by a space and cannot have extra spaces.

Input and Output Sample input example # #:
5ABCEDBDACEEBBAA
Sample # # of output:
1 0 3) 4 2
Description

For 30% of data, n<=10; is guaranteed

For 50% of data, n<=15; is guaranteed

For all data, the n<=26 is guaranteed.

The 4th question of noip2004 raising group

Code
1#include <iostream>2#include <cstring>3#include <algorithm>4#include <cstdio>5#include <vector>6#include <map>7 using namespacestd;8 9map<Char,int>m;Ten Chara[ *],b[ *],c[ *]; One intn,used[ -]; A  - voidSearch_ab (intRintt); - voidSearch_c (intRintsum); the  - intMain () { -Freopen ("01.in","R", stdin); -scanf"%d",&N); +scanf"%s%s%s", A +1, B +1, c+1); -      for(intI=0; i<=n;i++) m[Char('A'+i)]=0; +Search_ab (N,0); A } at  - voidSearch_ab (intRintt) { -     if(r==0){//Search Complete -          for(intI=0; i<n;i++) printf ("%d", m[Char('A'+i)]); -Exit0); -     } in      -      for(inti=r;i>=r;i--)if((m[a[1]]+m[b[1]]+2)%N < m[c[1]] )return; to //printf ("------"); + //for (int i=0;i<5;i++) printf ("%d", Used[i]); - //puts ("------"); the      *      for(inti=n-1; i>=0; i--){ $         if(!used[int(A[r])) {Panax Notoginseng             if(Used[i])Continue; -used[i]=used[int(A[r]) =1; them[a[r]]=i; +  A              for(intj=n-1; j>=0; j--){ the                 if(!used[int(B[r])) { +                     if(Used[j])Continue; -used[j]=used[int(B[r]) =1; $m[b[r]]=J; $                      - //printf ("%d%d%d", i,j,t); -Search_c (r,i+j+t); the                      -m[b[r]]=used[j]=used[int(B[r]) =0;//puts ("hh");Wuyi                 } the                 Else{ -                     if(M[B[R]]!=J)Continue; Wu //printf ("%d%d%d", i,j,t); -Search_c (r,i+j+t); About                 } $             } -              -m[a[r]]=used[i]=used[int(A[r]) =0; -         } A         Else{ +             if(m[a[r]]!=i)Continue; the              for(intj=n-1; j>=0; j--){ -                 if(!used[int(B[r])) { $                     if(Used[j])Continue; theused[j]=used[int(B[r]) =1; them[b[r]]=J; the                      the //printf ("%d%d%d", i,j,t); -Search_c (r,i+j+t); in                      them[b[r]]=used[j]=used[int(B[r]) =0;//puts ("hh"); the                 } About                 Else{ the                     if(M[B[R]]!=J)Continue; the //printf ("%d%d%d", i,j,t); theSearch_c (r,i+j+t); +                 } -             } the         }Bayi     } the } the  - voidSearch_c (intRintsum) { -     CharCh=C[r]; the      the     if(!used[int(C[r])) { the         if(used[sum%n])return; theused[sum%n]=used[int(C[r]) =1; -m[c[r]]=sum%N; the          the         intt= (sum-sum%n)/N; the         94 //cout<<t<<endl; theSearch_ab (R1, T); the          them[c[r]]=used[sum%n]=used[int(C[r]) =0;98     } About     Else{ -         if(m[c[r]]!=sum%n)return;101         102         intt= (sum-sum%n)/n;//Got a hole in here.103 //cout<<t<<endl;104Search_ab (R1, T);  the     }106}
90 min Qaq

Looks like, search upside down is certainly right?

In addition, the Line30 pruning will not output, it is unclear why

Rokua P1092 Insect Food calculation Label:dfs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.