CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Challenge 2013
    • May Cook-Off 2013
    • May Challenge 2013
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • Campus Chapters
    • Host your Contest
    • Go for Gold
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
    • Top Contributors on Discuss
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » March 2012 Challenge » Longest Weird Subsequence

Longest Weird Subsequence

Problem code: LWS

  • All Submissions

All submissions for this problem are available.

Finding the longest increasing subsequence is an old and well-known problem now. Here you will have to do something similar. You need to find the longest weird subsequence (LWS) of the given string. The subsequence is called weird if it can be split into two disjoint subsequences, one of which is non-decreasing and the other one is non-increasing.

Just for clarity, by subsequence of the given string S we mean any string that can be obtained from S by erasing from it zero or more characters. So empty string is a subsequence of any string and any string is a subsequence of itself. Further, note that we consider only strings composed of lowercase Latin letters and these letters compared by their ASCII codes. So, for example, 'a' is smaller than 'b' and 'p' is larger than 'h'.

Now let's consider some example. Let S="aabcazcczba". Then "abczz" is its some non-decreasing subsequene, "zccb" is its some non-increasing subsequence and "aabczcczba" is its some weird subsequence since it can be split into non-decreasing subsequence "aabzz" and non-increasing subsequence "cccba": "AABcZccZba" (first subsequence is shown by capital letters just for calrity).

Input

The first line contains a single positive integer T, the number of test cases. T test cases follow. The only line of each test case contains a non-empty string S composed of lowercase Latin letters.

Output

For every test case, output the length of the LWS of the given string.

Constraints

1 ≤ T ≤ 10
1 ≤ length of S ≤ 2000

Example

Input
3
abc
cbazyzabc
ddaabbaacc

Output
3
6
10

Explanation

First case: The string itself is LWS since it can be split into non-decreasing subsequence "abc" and non-increasing empty subsequence.

Second case: One of the possible LWS is "cbaabc" since it can be split as "cbaABC". Here we indicate by capital letters non-decreasing subsequence and by lowercase letters non-increasing one. Other possible LWS's are "cbaZZa", "AzyaBC".

Third case: Here the desired splitting is "ddAABBaaCC".


Author: vamsi_kavala
Tester: anton_lunyov
Editorial http://discuss.codechef.com/problems/LWS
Date Added: 29-07-2011
Time Limit: 3 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, FORT, FS, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, NODEJS, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TCL, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Can an empty string be

shadow @ 1 Mar 2012 07:13 PM
Can an empty string be considered as a non-decreasing or non-increasing subsequence?

@shadow: Yes, thus the answer

aseem_pandey @ 1 Mar 2012 08:37 PM
@shadow: Yes, thus the answer for first case

in 3rd case ddAABBaaCC ddBBaa

ashmish2 @ 2 Mar 2012 12:16 AM
in 3rd case ddAABBaaCC ddBBaa is non increasing AABBCC is non decreasing isnt it?

@ashmish2 : They need to be

abhimanyuma @ 2 Mar 2012 04:50 AM
@ashmish2 : They need to be disjoint...

@admin LWS for second case

dawnavd @ 2 Mar 2012 07:04 PM
@admin LWS for second case should be "cbayz" where "cba" is non increasing and "yz" is non decreasin, pls................. tell me why don'nt u considering it as LWS

@ashmish2: Please do not

vamsi_adm @ 2 Mar 2012 10:37 PM
@ashmish2: Please do not discuss answers! @dawnavd: LWS is LONGEST weird subsequence.

some other test cases please

akashsubhani @ 4 Mar 2012 01:46 PM
some other test cases please

@admin One tricky test case,

sukun007 @ 5 Mar 2012 09:56 PM
@admin One tricky test case, please.

will O(n*n) pass ??

allada @ 6 Mar 2012 10:27 AM
will O(n*n) pass ??

@manoharsingh23: Please do

vamsi_adm @ 6 Mar 2012 05:16 PM
@manoharsingh23: Please do not discuss your strategy. @allada: You should be able to figure that out looking at the constraints and the time limit.

@vamsi_adm : i regret if i

manoharsingh23 @ 6 Mar 2012 06:44 PM
@vamsi_adm : i regret if i was discussing my strategy in any way... btw that strategy is actually not working here... so i am just asking whether solutions exit around this strategy or not... because i was sure that it might work but it din't.. :P

@manoharsingh23: Yeah, you

vamsi_adm @ 6 Mar 2012 09:07 PM
@manoharsingh23: Yeah, you cannot do that either. You are NOT allowed to discuss any strategy, be it right or wrong,

@admin Hey! my code is

palak_agarwal @ 7 Mar 2012 02:02 PM
@admin Hey! my code is working for test cases given here + other possible test cases I am giving it. But I must be missing on some test case, that's why I am getting WA. Can you please post few other possible test cases....

an somebody tell meaning of

mpossible @ 7 Mar 2012 08:44 PM
an somebody tell meaning of disjoint in terms of this ???

Query: Anybody noticed that

ziyao_wei @ 7 Mar 2012 09:55 PM
Query: Anybody noticed that the date is from last year?

@mpossible: I guess disjoint

gultus @ 7 Mar 2012 10:01 PM
@mpossible: I guess disjoint means if you consider the indices of non-increasing and non-decreasing sub sequences they should not have any index in common.

@vamsi_adm : can there be

nikku @ 8 Mar 2012 08:55 PM
@vamsi_adm : can there be spaces in the string ?

@nikku: No.

vamsi_adm @ 8 Mar 2012 11:45 PM
@nikku: No.

over and over i'm getting

wRabbits @ 9 Mar 2012 04:00 PM
over and over i'm getting runtime using F#. not only in this problem. I'll code in C++ now, but can someone explain my mistake after the end of contest?

i m getting "wrong

kamaldeepst @ 9 Mar 2012 06:41 PM
i m getting "wrong answer"........can i know while or after competition.....for what case my algo was getting wrong??????? @admin

@vector9x and palak_agarwal:

vamsi_adm @ 10 Mar 2012 03:17 PM
@vector9x and palak_agarwal: Please do not discuss test cases.

So, contest ends. can someone

wRabbits @ 1 May 2012 11:05 PM
So, contest ends. can someone explain me my runtime error in f#? stack oveflow? please

SUCCESSFUL SUBMISSIONS


Fetching successful submissions

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.