[Programming questions] never thought of clever editing Golang state machine

[Programming questions] never thought of clever editing

Time limit: C / C ++ 1 second, 2 seconds of space restrictions in other languages: C / C ++ 32M, 64M other languages
I am Wang sledgehammer, an editor for a publishing house. I was responsible for proofreading the manuscript submission to English, this work is very annoying, because many have to fix spelling mistakes every day. But good people can always find the truth in the ordinary work. I found a shortcut to find misspelled:

  1. The same three letters together, must be misspelled, the removal of one thousand million: such helllo -> hello
  2. As two pairs of letters (the AABB type) are linked together, must be spelling errors, remove a second pair thousand million letters: such helloo -> hello
  3. The above rule priority "left to right" match, that if it is AABBCC, although AABB and BBCC are misspellings, should give priority to repair the AABB, the result is AABCC

I especially meow is a genius! I studied at Lanxiang had excavator and programming, according to this principle wrote an automatic verifier, taking off from work efficiency. Pretty soon, I will serve as CEO, became chairman of the board, marry white Formica, took to the pinnacle of life, think about all a little excited about it!
I never thought I was fired, and before leaving the boss said to me:. "Doing things to be dedicated, diligent, the sub-points, if the person line, line by line, line by line dry trekking trekking; and if not, dry No line of his party, his party will not do trekking not. "I am now the whole person booming trance ......

Please listen to the question: Please realize sledgehammer automatic proofreading program

Input Description:
The first line comprises a number N, the number of this string to be verified with the embodiment comprises.

Followed by N rows, each of the acts to be a parity string.

Output Description:
N rows, each row comprising a string to be repaired.
Example 1
Input

2
helloo
wooooooow

Export

hello
woow

Thinking

Here is the method I use state machine, but not the optimal solution, look on the line, just a habitual record it.
Others use a double pointer situ modified algorithm is more excellent, copy others, you might want to comment on their own subject area learning.

answer

package main
 
import "fmt"
 
func main() {
    var n int
    var str string
    fmt.Scan(&n)
    for ;n>0;n--{
        fmt.Scan(&str)
        if len(str)<=2{
            fmt.Println(str)
        }
        flag:=0
        tmp:=string(str[0])
        for i:=1;i<len(str);i++{
            switch flag {
            case 0:
                tmp=tmp+string(str[i])
                if str[i]==str[i-1]{
                flag=1
            }
            case 1:
                if str[i]!=str[i-1]{
                    tmp=tmp+string(str[i])
                    flag=2
                }
            case 2:
                if str[i]!=str[i-1]{
                    tmp=tmp+string(str[i])
                    flag=0
                }
            }
        }
        fmt.Println(tmp)
    }
}
Published 38 original articles · won praise 0 · Views 1031

Guess you like

Origin blog.csdn.net/Cyan1956/article/details/104804609