ARTS eighth week punch

Algorithm: Make a leetcode algorithm problem

13. Roman numerals to Integer

Roman numeral characters comprising the following seven: I, V, X, L, C, D and M.

Numerical character
the I. 1
V. 5
X-10
L 50
C 100
D 500
M 1000
, for example, 2 written as Roman numerals II, namely 1.12 written as two parallel XII, namely X + II. 27 written as XXVII, namely XX + V + II.

Typically, small numbers of Roman numerals to the right in large numbers. But there are exceptions, for example, do not write 4 IIII, but IV. In the left number 5 number 1, the number of large numbers equal to the value represented by a reduced number 5 obtained 4. Likewise, the number 9 is represented as IX. This special rule applies only to the following six cases:

I may be on the left V (5) and X (10), and to represent 4 and 9.
X L can be placed on the left (50) and C (100), and 40 and 90 are represented.
C may be placed on D (500) and M (1000) to the left, to 400 and 900 represent.
Given a Roman numeral, to convert it to an integer. To ensure that the input is in the range of 1 to 3999

Example 1:

Input: "III"
Output: 3
Example 2:

Input: "IV"
Output: 4
Example 3:

Input: "IX"
Output: 9
Example 4:

Input: "LVIII"
Output: 58
explains: L = 50, V = 5 , III = 3.
Example 5:

Input: "MCMXCIV"
Output: 1994
explanation: M = 1000, CM = 900 , XC = 90, IV = 4.

Problem-solving ideas: 

    1 . Construction of a dictionary record all substrings Roman numerals, note that the length of the substring is recorded a value 2 (actual value - left substring within a value represented in Roman numerals) 

int RomanToInt ( String S) 
{ 
    unordered_map < String , int > STMAP = {{ " the I " , . 1 }, { " IV " , . 3 }, { " IX " , . 8 }, { " V " , . 5 }, { " X- " , 10 }, 
    { " XL " ,30}, {"XC", 80}, {"L", 50}, {"C", 100}, {"CD", 300}, {"CM", 800}, {"D", 500}, {"M", 1000}};

    int iSum = stMap[s.substr(0, 1)];

    for (int i = 1; i < (int)s.size(); i++)
    {
        string szOne = s.substr(i, 1);
        string szTwo = s.substr(i - 1, 2);
        iSum += stMap[szTwo] ? stMap[szTwo] : stMap[szOne];
    }

    return iSum;

}


Review: Read the article in English technical articles and reviews

Guide to MySQL High Availability

Data is the currency of today's web, mobile, social, enterprise and cloud applications. Ensuring data is always available is a top priority for any organization - minutes of downtime will result in significant loss of revenue and reputation.

Data is today's networks, mobile phones, social, enterprise and cloud applications currency; ensure that data is always available is the primary task of any organization - a few minutes downtime will result in lost revenue and reputation seriously.

This Guide is designed to assist Developers, Architects and DBAs understanding, implementing and managing MySQL InnoDB Cluster, an integrated, native, HA solution for your MySQL databases.


Tips: learning a technical skill

Original link: https://zhuanlan.zhihu.com/p/36274119

C ++ a pit face questions: STL sort algorithm used in what sort algorithm?


Not all containers use the sort algorithm

      Since the question is the STL sort algorithm, then make sure a problem, which containers need to use STL sort algorithm?
First, the relationship type container has an automatic sorting function (relative key value) , because the underlying using RB-Tree, there is no need to use sort algorithms.
Secondly, the sequence of Formula container stack, queue priority-queue, and has a specific entrance, it does not allow users to sort the elements.
The remaining vector, deque, applicable sort algorithm.


Implementation logic

The STL sort algorithm, when the amount of data QuickSort row fast algorithm , merge sort segment. Once the segmented data amount less than a certain threshold (16), in order to avoid fast row QuickSort recursive call causing too much additional load on the switch Insertion Sort insertion sort . If the level of recursion too deep, will use HeapSort heap sort .


具体代码
源文件:/usr/include/c++/4.2.1/bits/stl_algo.h

template <class _RandomAccessIter, class _Compare>
template <class _RandomAccessIter, class _Compare>
inline void sort(_RandomAccessIter __first, _RandomAccessIter __last,
                 _Compare __comp)
{
    __STL_REQUIRES(_RandomAccessIter, _Mutable_RandomAccessIterator);
    __STL_BINARY_FUNCTION_CHECK(_Compare, bool,
                                typename iterator_traits<_RandomAccessIter>:: the value_type, 
                                typename iterator_traits <_RandomAccessIter> :: the value_type);
     IF (! __first = __last) 
    { 
        // Quicksort + heap sort 
        __introsort_loop (__ First, __last, 
                         __VALUE_TYPE (__ First), 
                         __lg (__ Last - __first) * 2 , 
                         __comp ); 

        // insertion sort (sorted when the number of <= 16) 
        __final_insertion_sort (__ First, __last, __comp); 
    } 
} 

Template < class _RandomAccessIter, class _TP, class _size,class _Compare>
 void __introsort_loop (_RandomAccessIter __first, 
                      _RandomAccessIter __last, _TP * , 
                      _size __depth_limit, _Compare __comp) 
{ 
    // if the number is greater than the ordering 16 
    the while - (__last __first> __stl_threshold) 
    { 
        // recursive depth is 0, calls heapsort 
        IF (__depth_limit == 0 ) 
        { 
            partial_sort, (__ First, __last, __last, __comp); 
            return ; 
        }

         - __depth_limit; 

        // quick sort
         // 1. find the reference element
        _RandomAccessIter __cut = 
            __unguarded_partition (__ First, __last, 
                                  _TP (__ Median ( * __first,
                                                * (__ First + (__last - __first) / 2 ),
                                                * (__ Last - . 1 ), __comp)), 
                                  __comp); 

        // partition Thought: Recursive sorting 
        __introsort_loop (__ Cut, __last, (_TP *) 0 , __depth_limit, __comp); 
        __last = __cut; 
    } 
} 


// hEAPSORT 
Template < class _RandomAccessIter,class _Tp, class _Compare>
void __partial_sort(_RandomAccessIter __first, _RandomAccessIter __middle,
                    _RandomAccessIter __last, _Tp *, _Compare __comp)
{
    make_heap(__first, __middle, __comp);
    for (_RandomAccessIter __i = __middle; __i < __last; ++__i)
        if (__comp(*__i, *__first))
            __pop_heap(__first, __middle, __i, _Tp(*__i), __comp,
                       __DISTANCE_TYPE(__first));
    sort_heap(__first, __middle, __comp);
}

// 插入排序
template <class _RandomAccessIter, class _Compare>
void __final_insertion_sort(_RandomAccessIter __first,
                            _RandomAccessIter __last, _Compare __comp)
{
    if (__last - __first > __stl_threshold)
    {
        __insertion_sort(__first, __first + __stl_threshold, __comp);
        __unguarded_insertion_sort(__first + __stl_threshold, __last, __comp);
    }
    else
        __insertion_sort(__first, __last, __comp);
}

// 计算递归深度
template <class _Size>
inline _Size __lg(_Size __n)
{
    _Size __k;
    for (__k = 0; __n != 1; __n >>= 1)
        ++__k;
    return __k;
}


Share: Share ideas and think there is a technical articles

Original link: https://zentia.github.io/2018/09/21/TSF4G/

TBUS principles and implementation, so that ignores network communications;


TBUS implementation principle:

Tbus based on shared memory constructed without locking the two-cycle message queue , both the completion of reading and writing data transceiver via a dedicated transmission queue, to achieve local interprocess communication or remote process. Two queues called tbus channel (channel) used in both the communication, each set of communication parties need to have a tbus channel.


On the same physical machine communication:

image


Communication between different physical machines:

image

Guess you like

Origin www.cnblogs.com/yzdai/p/11409359.html