An algorithm for strings

Source: Internet
Author: User
An algorithm for strings
Suppose I have such a string:
Asda2121ffasas Hello, hello, hello, hello, hello. 22111000csdnsdn



The above string should be treated as:
Asda2121ffasas Hello, hello, hello. 221100csdnsdn


Other words:
In this string, any successive occurrences of a character or substring cannot be greater than two times, otherwise it is displayed only two times.

The worst algorithm is to make the string a layer of convenience, the number of each character and substring appears, there are more than 2 to be processed. But if the string is very long, the efficiency is too low. Ask the optimization algorithm. Thank you.


In fact, this is our site planning to give me a demand, because there are a lot of users in the message when always write some repetitive message, he wants to let these messages only show a certain number of unnecessary will not show. I've called him back because if the worst-case algorithm is too inefficient, you still want to know if there are any more efficient algorithms.

------Solution--------------------
After the word breaker, and then go through the string, basically and what you say is not efficient, if placed in the server is more deadly, if you really want to implement this method, it is recommended that you put it in the client with JS implementation.

When the user edits the text, monitoring the other side does not judge Ctrl + C and CTRL + V, you give it to play the window, "man, csdn can not irrigation, can not reply to the content is too short oh ... $&^#%&*" ”
------Solution--------------------
$s = ' Asda2121ffasas hello hello, hello hello, hello, 22111000csdnsdn ';
echo preg_replace ('/(. +) \\1+/', ' $1$1 ', $s);

Out
Asda2121ffasas Hello, hello, hello. 221100csdnsdn


$s = ' Asda2121ffasas Hello, hello, good, hello, Hi, hello. 22111000csdncsdncsdnsdn ';
echo preg_replace ('/(. +) \\1+/', ' $1$1 ', $s);

Out
Asda2121ffasas Hello, hello, good. Hello, hi. 221100csdncsdnsdn
------Solution--------------------
This algorithm is not so simple. O (n) estimated the chance.
The second result of the nagging example was 3 times ' good '.
Match even if you join Zhong Wenjing
The problem is something like this.
$str = ' 221112211122111 ';//' 22111 ' 3 times, ' 1 ' 3 times
What do you expect from the final processing results? 22112211?

------Solution--------------------
#23楼的思路回到while循环那去了, hehe.
Just like I think.
PHP Code
$s = ' I am you, you are me, I am you, you are me, you are me, you are me. I am you, you are me, I am you, you are me, I am you, you are me, I am you, you are me, you are me, I am you, you are me, I am you, I am you, I am you, you are me, you are me, you are me, you are me, $str = Preg_replace ('/.) \\1{2,}/', ' $1$1 ', $s); while (Preg_match ('/(. +?) \\1{2,}/', $str)) {    $str = preg_replace ('/(. +?) \\1{2,}/', ' $1$1 ', $str);} Echo $str;
  • 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.