[2-2] lexicographical problem

'Description of the problem:
the need for a special string encoded data encryption and data compression often. Given by the 26 alphabet A small
write English letters A = {a, b, ... , z}. The resulting alphabet ascending string refers to a string of letters from the left
in the same order with the right order of occurrence of the letters appear in the alphabet, and each character appears at most once. For example,
A, B, ab &, BC, XYZ, etc. are ascending string string. All now alphabet A length by no more than 6 in ascending
string are arranged and coded as lexicographic order.
26 is 27 2 ... 28. 1 ...
ab & ... Z ... ab & AC
for any string length does not exceed 6 ascending rapidly calculate its encoding in the above-described dictionary.
'Programming tasks:
for a given string length does not exceed 6 in ascending order, it is encoded in the program to calculate the above-described dictionary.
'Input data:
the input data file by a text file called input.txt.
The first line of the file is a positive integer k, k represents the total of the next line.
The next k rows, each row is given a string.
'Output Results:
At the end of run, outputs the calculation result to the file output.txt. A total file k rows, each row corresponding to a
coded string of.
Sample output file input file Example
input.txt output.txt
2
A
B
. 1
2

【answer】


A null string is started.
Then the length of the string inside the i-1 s taken out of order lexicographically
and then later add a s suitable character generator to a string of i.
Finally, again traversing each string with reference to a map

[Code]

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <unordered_map>
using namespace std;

vector <string> v[7];
unordered_map<string,int> dic;

int main(){
    v[0].push_back("");
    for (int i = 1;i <= 6;i++){
        int len = v[i-1].size();
        for (int j = 0;j < len;j++){
            string temp = v[i-1][j];
            int len2 = temp.size();
            char key = 'a';
            if (temp!=""){
                key = temp[len2-1]+1;
            }
            for (char q = key;q<='z';q++){
                v[i].push_back(""+temp+q);
            }
        }
    }
    int cur = 0;
    for (int i = 1;i <= 6;i++){
        int len = v[i].size();
        for (int j = 0;j < len;j++){
            dic[v[i][j]] = ++cur;
        }
    }
    int k;
    cin >> k;
    while (k--){
        string s;
        cin >> s;
        cout<<dic[s]<<endl;
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/AWCXV/p/11615595.html