The 15th offline real-time how to write reference problem in Python

Click here for details. http://qiita.com/Nabetani/items/0b56395d4c9e7c64b230 I referred to saihon2013's C language implementation.

def search(text, left, right, match):
    longer = match
    for l in xrange(left, len(text)):
        for r in xrange(right, l-1, -1):
            if text[l] == text[r]:
                m = search(text, l+1, r-1, match + (1 if l==r else 2))
                if m > longer: longer = m
                break
    return longer

def solve(text):
    return search(text, 0, len(text)-1, 0)

def test(text, answer):
    result = solve(text)
    print 'OK' if result==int(answer) else "NG", result, answer, text

if __name__ == '__main__':
    0, test( "I_believe_you_can_solve", "9" );
    1, test( "a", "1" );
    2, test( "aa", "2" );
    3, test( "aaa", "3" );
    4, test( "ab", "1" );
    5, test( "aabb", "2" );
    6, test( "ABBA", "4" );
    7, test( "Abba", "2" );
    8, test( "1234567890", "1" );
    9, test( "1234567890987654321", "19" );
    10, test( "abcdcba", "7" );
    11, test( "0a1b2c3d4c5b6a7", "7" );
    12, test( "abcdcba0123210", "7" );
    13, test( "abcdcba_123210", "7" );
    14, test( "_bcdcba0123210", "7" );
    15, test( "abcddcba0123210", "8" );
    16, test( "abcdcba01233210", "8" );
    17, test( "a0bc1dc2ba3210a", "9" );
    18, test( "a0bc1ddc2ba3210", "8" );
    19, test( "3a0bc1ddc2ba3210", "10" );
    20, test( "11oooo1111o1oo1o111ooooooooooo", "20" );
    21, test( "11o1111o1111oo11ooo11111ooo1oo", "20" );
    22, test( "111111oo11o111ooo1o1ooo11ooo1o", "21" );
    23, test( "11o1o1o11oo11o11oo111o1o1o11oo", "27" );
    24, test( "oo111o1o11o1oo1ooo11o1o11o1o1o", "27" );
    25, test( "1o1oo11111o1o1oo1o1o1111oo1o1o", "28" );
    26, test( "QQooooQooooQQyQoyQQQyyyyQQoyoy", "15" );
    27, test( "oQoooQooooQyoyQoyoyyyQQyQQQQoQ", "16" );
    28, test( "yyyyyooyQyyyoyyQyyooyQoQoQQoQy", "17" );
    29, test( "yyQoyQoyyQyQQoyooooyyQQyQyooQy", "24" );
    30, test( "oQQooQoQyQQoyoQQoQyQyQyQoQoooo", "24" );
    31, test( "oQQyQQyyQyQQoooyQQyyyQQQyyQQoy", "25" );
    32, test( "WAk9iHI4jVDlStyudwTNqE138kwan2", "3" );
    33, test( "c43fIu1Mlz0K9hEVOgGcUdbeB5ksa7", "3" );
    34, test( "CAbYcW5VqHjzFdIkH_61PM0TsyRuie", "3" );
    35, test( "jInQnUvSayrJTsQWujovbbqW0STvoj", "10" );
    36, test( "iZDrvpUKgtj3FrZsZ4CLjrEgUdZzQG", "11" );
    37, test( "ROgYUOu6er_DA7nB6UGquO1GUHC62R", "11" );
    38, test( "Oh_be_a_fine_girl_kiss_me", "9" );
    39, test( "8086_6502_6809_Z80", "11" );
    40, test( "xcode_visualstudio_eclipse", "11" );
    41, test( "word_excel_powerpoint_outlook", "9" );
    42, test( "abcdefghijklmnopqrstuvwxyz0123", "1" );

Recommended Posts

The 15th offline real-time how to write reference problem in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
The 17th offline real-time how to write reference problem implemented in Python
The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
The 16th offline real-time how to write problem was solved with Python
The 15th offline real-time how to write problem was solved with python
The 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
The 15th offline real-time I tried to solve the problem of how to write with python
The 14th offline real-time writing reference problem with Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
13th Offline Real-time How to Solve Writing Problems in Python
The 10th offline real-time writing reference problem. Implementation example by Python.
Offline real-time how to write Python implementation example of E15 problem
The 11th offline real-time writing reference problem. Implementation example by python.
Answer to "Offline real-time how to write F02 problem"
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
Offline real-time how to write Python implementation example of E14
How to write offline real-time Solving E05 problems with Python
The twelfth offline real-time writing reference problem. Implementation by python
How to write Ruby to_s in Python
Offline real-time how to write E11 ruby and python implementation example
How to write offline real time Solve E04 problems in Python
How to write offline real time I tried to solve the problem of F02 with Python
How to use the C library in Python
The trick to write flatten concisely in python
How to get the files in the [Python] folder
How to write the correct shebang in Perl, Python and Ruby scripts
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Basic information Write the 2018 fall algorithm problem in Python
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to know the current directory in Python in Blender
I want to write in Python! (3) Utilize the mock
How to use the model learned in Lobe in Python
How to develop in Python
[Python] How to output the list values in order
[Note] How to write QR code and description in the same image with python
How to write custom validations in the Django REST Framework
[python] How to check if the Key exists in the dictionary
How to debug the Python standard library in Visual Studio
How to use the __call__ method in a Python class
[Python] How to write an if statement in one sentence.
How to get the last (last) value in a list in Python
How to write a Python class
[Python] How to do PCA in Python
How to write soberly in pandas
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
How to get the Python version
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python