Coding Problem (Java) __ algorithm

Source: Internet
Author: User

1. Title:

The topic Description assumes that a coded range is 25 letters of a ~ y, from 1-bit to 4-bit encoding, if we sort the encoding in dictionary order, form an array as follows: A, AA, AAA, AAAA, Aaab, AAAC, ..., b, Ba, BAA, b AAA, Baab, Baac ..., yyyw, Yyyx, yyyy one of the index for 0,AA of the index is 1,AAA index is 2, and so on. Write a function, input is any one encoding, output this encoding corresponding to the index. Enter a description:

Enter a string to be encoded, with a string length less than or equal to 100.
Output Description:
Output the index of this encoding
Example 1 input
Baca
Output
16331


2, the topic Analysis:

is actually a little bit of added. This is a little bit of meaning: start with the first analysis, until the end of the string.
Here's a little bit of math, the range expression: [X,y] An expression that represents all the numbers in a range, and includes the number x in the left range
, excluding the number Y in the right range. Then in the following solution, we use one of these expressions to find the number of expressions in a range of
strings.


Here is an example to understand.


Example:
bdcd

First: b
now, let's ask [a,b] how many strings are in this range.
a:1
ax:25
axx:25*25
axxx:25*25*25
The number of strings with a range of [a,b) is: Math.pow (25,3) +math.pow (25,2) + Math.pow (25,1) +1 = 16,276

second digit: D
Now, let's ask the number of strings in this range [B,BD].
b:1

ba:1
bax:25
baxx:25*25
bb:1
bbx:25 bbxx:25*25 bc:1 bcx:25
bcxx:25*25
The number of [B,BD] strings is: 1+3* (Math.pow (25,2) +math.pow (25,1) +1) = 1954


Third: C
now, we ask [BD, BDC) is the number of strings in this range.
bd:1

bda:1
bdax:25
bdb:1
bdbx:25
The number of strings to [Bd,dbc): 1+2* (Math.pow (25,1) +1) = 53

Fourth: D
Now, let's ask the number of strings in the range [BDC,BDCD].
bdc:1

bdca:1
bdcb:1
bdcc:1
bdcd:1
the number of string digits in the range of [BDC,BDCD]: 1+4* (1) = 5

So BDCD is the 18,288th character, because the count of the characters is starting from 0, so the BDCD subscript should be bit 18287


3, Code:

Package schooloffer17;

Import Java.util.Scanner;

/**
 * Created by Caoxiaohong on 17/11/19 10:54.
 * < encoding ></>/public
class Coding {public
    static void Main (string[] args) {
        Scanner scanner= New Scanner (system.in);
        String str;
        while (Scanner.hasnext ()) {
            str=scanner.nextline ();
            int res=0;
            int len=str.length ();
            res+= (Str.charat (0) -97) * (Math.pow (25,3) +math.pow (25,2) +25+1);
            if (len>1)
                res+= (Str.charat (1) -97) * (Math.pow (25,2) +25+1) +1;
            if (len>2)
                res+= (Str.charat (2) -97) * (25+1) +1;
            if (len>3)
                res+= (Str.charat (3) -97) *1+1;
            System.out.println (res);}}



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.