To find the largest increment subsequence in a string

Source: Internet
Author: User

Database environment: SQL SERVER 2005

Title, the largest increment subsequence in the string "Abcbklmnodfghijkmer" is obtained. This string is a bit special,

Consists of only 26 lowercase letters A-Z.

The approximate idea is as follows:

1. Move the string to a column store and generate the line number

2. Set an increment counter column, default to 1, to compare the characters of the upper and lower lines, if the order in the dictionary is incremented,

The counter adds 1, otherwise the counter is 1

3. Find the maximum number of counters and corresponding line numbers, and intercept the strings based on these 2 numbers

The idea has, the following directly paste code

DECLARE @vtext VARCHAR(255)SET @vtext = 'Abcbklmnodfghijkmer'/*tells the string to be stored in a column and generates a line number*/ withx0 as(SELECT    Number  asID,SUBSTRING(@vtext, Number,1) as Letter frommaster.dbo.spt_valuesWHEREType= 'P'                         and  Number <= LEN(@vtext)                         and  Number >= 1             ),/*Implementing Counters*/X1 (ID, letter, Clen) as(SELECTID, letter,1  asClen fromx0WHEREId= 1               UNION  All               SELECTx0.id, X0.letter, Case  whenX1.letter<=X0.letter ThenX1.clen+ 1                             ELSE 1                        END  asClen fromx0, X1WHEREX0.id=X1.id+ 1             )    /*Intercept String*/    SELECT  SUBSTRING(@vtext, Start, Sublen) asMaximum sub-sequence from(SELECTID, Clen,MAX(Clen) Over( ) asMaxclen, id- MAX(Clen) Over( )+ 1  asStart,MAX(Clen) Over( ) asSublen fromx1) TWHEREClen=Maxclen

The maximum number of sub-sequences to be calculated is

(End of this article)

To find the largest increment subsequence in a string

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.