codevs1322 Word Matrix

Source: Internet
Author: User

Title Description Description

For the word s that contain the letters A through Y, write them from top to bottom, from left to right in a 5*5 matrix, such as a single
The word adjptbekqucglrvfinswhmoxy is written as follows:
A D J P T
B E K Q U
C G L R V
F I N S W
H M O X Y
If the matrix satisfies each row of each column the letter is the dictionary order increment is said to be graceful, as the above word is
Graceful, while the adjptbegqucklrvfinswhmoxy is not (the second column does not meet the requirements).
Your Task
Sequence all the beautiful words in a dictionary, from small to large numbered,......
Please complete the following two types of tasks:
1. Given a beautiful word, ask for its number.
2. Given a number, find the corresponding beautiful word.

Enter a description input Description

The first line is a letter, w means the task 1,n represents the Task 2
If task 1, the second line is a graceful word, otherwise the second line is a positive integer, indicating a beautiful single
The number of words to ensure that the number does not exceed the number of graceful words

outputs description output Description

Line, if the task 1, output corresponding number, otherwise the output corresponding to the beautiful word

sample input to sample

W
Abcdefghijklmnopqrsutvwxy

Sample output Sample outputs

2

data size & Hint

Sample Input Two
N
20
Sample Output Two
Abcdefghijklmnopqsuwrtvxy

Positive Solution: Memory SearchProblem Solving Report:Today the exam T5, feel good god,%%%. considering a direct search would definitely be tle, but we can think of a point that is certainly smaller than the lower right of him, which can be used as a pruning. then consider the memory search, F[I][J][K][L1][L2] said the first line put the I, the second row of J, the third row of K, the fourth line of L1, the fifth line of the L2 of the program number (to ensure that five numbers do not fall, to ensure legitimacy)and how do we do it? Take the n operation as an example, because we want to get the nth string, so we can do this: first of all, from 1 to 25 to determine what to fill, such as the first three to fill in ABC, and then I calculate the first three bit fixed case I counted the total number of scenarios, if greater than N, Then the first three bits of ABC is feasible (because if it is already greater than N, then this one will be more big is not legal, so you can be sure that this is the number of the current search), and then continue to look down until I find that when I am certain that a discovery is less than n, then I will reduce the number of programs from N, Then I will make this one slightly bigger, and then the statistical scheme, constantly repeated. The final sequence is the sequence I want. W operations are similar, except that the upper bound of each constraint must not exceed the given string, the final statistical scheme, the output scheme +1 (to include itself).
1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - using namespacestd; -typedefLong LongLL; the intN; - Charcc,ch[ -]; - intans[ -]; - intf[6][6][6][6][6]; + //F[i][j][k][l1][l2] said that the first line placed I, the second row of J, the third row of K, the fourth row of L1, the fifth line of L2 a number of programs, to ensure that five numbers do not fall, to ensure legitimacy -  +InlineintGetint () A { at        intw=0, q=0;CharC=GetChar (); -         while((c<'0'|| C>'9') && c!='-') C=getchar ();if(c=='-') q=1, c=GetChar (); -         while(c>='0'&& c<='9') w=w*Ten+c-'0', C=getchar ();returnQ? -w:w; - } -  -InlineBOOLCheckintXintNUM) {return(!ans[x]) | | (ans[x]==num); } in  -InlineintDfsintAintBintCintDintEinttot) { to     if(tot== -)return 1; +     int&tmp=F[a][b][c][d][e]; -     if(TMP)returntmp; the     if(a<5&& Check (a,tot+1)) Tmp+=dfs (A +1, b,c,d,e,tot+1);//fill in the first line *     if(B<a && Check (b +5, tot+1)) Tmp+=dfs (a,b+1, c,d,e,tot+1);//guaranteed to be strictly non-descending $     if(C<b && Check (c+Ten, tot+1)) Tmp+=dfs (a,b,c+1, d,e,tot+1);Panax Notoginseng     if(D<c && Check (d+ the, tot+1)) Tmp+=dfs (a,b,c,d+1, e,tot+1); -     if(E<d && Check (e+ -, tot+1)) Tmp+=dfs (a,b,c,d,e+1, tot+1); the     returntmp; + } A  theInlinevoidWork () { +Cc=GetChar (); -     if(cc=='W') { $scanf"%s", CH);  $      for(intI=0;i< -; i++) { -          for(ans[i]=1; ans[i]<ch[i]-'A'+1; ans[i]++) { -Memset (F,0,sizeof(f)); theN+=dfs (0,0,0,0,0,0); -         }Wuyi     } theprintf"%d", n+1);//Add yourself -     } Wu     Else { -N=getint ();intNow ; About      for(intI=0;i< -; i++) { $          for(ans[i]=1; ans[i]<= -; ans[i]++) {//determine the status, search for the number of answers in the current state -Memset (F,0,sizeof(f));//empty at any time -Now=dfs (0,0,0,0,0,0); -         if(now>=n) Break; An-=Now ; +         } the     } -      for(intI=0;i< -; i++) printf ("%c", ans[i]+'A'-1); $     } the } the  the intMain () the { - Work (); in   return 0; the}

codevs1322 Word Matrix

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.