Codevs 1064 Insect Food Count 2004 Noip National League Improvement Group

Source: Internet
Author: User
Tags first row time limit

1064 Insect Food Count 2004 years Noip National League Improvement Group
Time limit:2 S
Space limit: 128000 KB
Title Level:Diamond Diamonds
Title Description 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
44445506978
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
DCCC
The above equation 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. The input data is guaranteed to have only one set of solutions,
Enter a description input Description
The input contains 4 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.
Outputs description Output Description
The output contains one 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.
Sample input to sample
5
abced
Bdace
Ebbaa
Sample output Sample Outputs
1 0 3) 4 2
Data Size & Hint
For 30% of data, n<=10; is guaranteed
For 50% of data, n<=15; is guaranteed
For all data, the n<=26 is guaranteed.
Category Label Tags
Depth First Search search Mainland region Noip National League increase Group 2004

/* did not expect pruning during the exam.
Only 40 cents Qwq.
*/#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #define MAXN 30
using namespace Std;
int S[4][MAXN],C[4][MAXN];
int N,A[MAXN];
BOOL B[MAXN];
    int read () {int X=0,f=1;char ch;ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar ();
return x*f;
    } void Check () {int p=0;
      for (int. i=1;i<=3;i++) for (int j=1;j<=n;j++) {c[i][j]=a[s[i][j]];
        } for (int j=n;j>=1;j--) {p= (c[1][j+1]+c[2][j+1]+p)/n;
      if ((c[1][j]+c[2][j]+p)%n!=c[3][j]) return;
    } for (int i=1;i<=n-1;i++) {printf ("%d", a[i]);
    } printf ("%d", a[n]);
Exit (0);
    } void Dfs (int t) {if (t==n+1) {check (); return;}
for (int i=0;i<n;i++) if (!b[i]) B[i]=1,a[t]=i,dfs (t+1), b[i]=0;
    } void Slove () {DFS (1);} int main () {n=read (); Char ch;
      for (int i=1;i<=3;i++)for (int j=1;j<=n;j++) {cin>>ch;s[i][j]=ch-64;
    } slove ();
return 0; }
/* Add pruning when searching for the whole arrangement with the obvious pruning.
By test instructions: When we all know whether a bit is being rounded, we can only determine a set of answers.
Determine if the current letter has been misjudged before.
The maximum number of rounding is 1. So direct (a+b)%n==c| |
(a+b+1)%n==c;
From right to left, search in alphabetical order.
*/#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #define MAXN 30
using namespace Std;
int S[4][MAXN],C[4][MAXN],W[MAXN*MAXN];
int N,a[maxn],tot;
BOOL B[MAXN];
    int read () {int X=0,f=1;char ch;ch=getchar (); while (ch< ' 0 ' | |
    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
    while (ch>= ' 0 ' &&ch<= ' 9 ') X=x*10+ch-48,ch=getchar ();
return x*f;
    } void Check () {int p=0;
        for (int j=n;j>=1;j--) {p= (a[s[1][j+1]]+a[s[2][j+1]]+p)/n;
      if ((A[s[1][j]]+a[s[2][j]]+p)%n!=a[s[3][j]]) return;
    } for (int i=1;i<=n-1;i++) {printf ("%d", a[i]);
    } printf ("%d", a[n]);
Exit (0);
            } bool JD () {for (int i=n;i>=1;i--) {if (a[s[1][i]]!=-1&&a[s[2][i]]!=-1&&a[s[3][i]]!=-1) { if ((a[s[1][i]]+a[s[2][i]]+1)%n==a[s[3][i]]| | (A[s[1][i]]+a[S[2][i]])
            %n==a[s[3][i]]) continue;
        return false;
}} return true;
    } void Dfs (int t) {if (t==n+1) {check (); return;}
        for (int i=0;i<n;i++) {if (!b[i]) {b[i]=1,a[w[t]]=i;
        if (JD ()) DFS (t+1);
        B[i]=0;a[w[t]]=-1;
    }}} void Slove () {DFS (1);} int main () {n=read (); Char ch;
        for (int i=1;i<=3;i++) for (int j=1;j<=n;j++) {cin>>ch;s[i][j]=ch-64;
          if (!a[ch-64]) {a[ch-64]=-1;w[++tot]=ch-64;
    }} slove ();
return 0; }

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.