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 » May Cook-Off 2012 » Little Elephant and Swapping

Little Elephant and Swapping

Problem code: LUCKYSWP

  • All Submissions

All submissions for this problem are available.

A Little Elephant from the Zoo of Lviv likes lucky strings, i.e., the strings that consist only of the lucky digits 4 and 7.

He is now studying some special transformation defined on the set of the lucky strings. The lucky string T is called a swap permutation of the lucky string S with |S| = N if it can be derived from S by the following process.
  • Choose some integers L and R such that 1 ≤ L ≤ R ≤ N.
  • Denote by A the substring S[L, R] and by B the concatenation S[1, L - 1] + S[R + 1, N].
  • Choose some integer K such that 0 ≤ K ≤ |B|.
  • Put T = B[1, K] + A + B[K + 1, |B|].

In other words, T is a swap permutation of S if it can be obtained from S by deleting some non-empty substring from S and then inserting it back into any position of S. Note that S is always a swap permutation of itself.

Denote by F(S) the length of the longest non-decreasing subsequence of S. In other words, this subsequence should have one of the following forms: 444...444, 777...777, 444...444777...777.

The Little Elephant has the lucky string S and as an experienced theoretical scientist he is interested in some quite theoretical problem. Namely, he wants to find the maximal value of F(T) if T can be an arbitrary swap permutation of S. Help him and find this value.

Notes.

Let S be some lucky string. Then
  • |S| denotes the length of the string S;
  • S[i] (1 ≤ i ≤ |S|) denotes the ith character of S (the numeration of characters starts from 1);
  • S[L, R] (1 ≤ L ≤ R ≤ |S|) denotes the string with the following sequence of characters: S[L], S[L + 1], ..., S[R], and is called a substring of S. For L > R we mean by S[L, R] an empty string.

For any two lucky strings S and T their concatenation is defined as the sequence of characters in S followed by the sequence of characters in T, and is denoted by S + T.

The string T is called a subsequence of the string S if T can be derived from S by deleting some (possibly zero) number of characters without changing the order of the remaining characters. For example, T = 474 is a subsequence of S = 74477747 since after deleting characters at positions 1, 2, 5, 6, 8 from S we obtain T. Note that, the empty string and the string S itself are always the subsequences of S.

Input

The first line of the input file contains a single integer T, the number of test cases. Each of the following T lines contains one string, the string S for the corresponding test case. The input file does not contain any whitespaces.

Output

For each test case, output a single line containing the answer for this test case, that is, max{F(T) : T is a swap permutation of S}.

Constraints

1 ≤ T ≤ 10

1 ≤ |S| ≤ 100000

S consists only of the lucky digits 4 and 7.

Example

Input:
5
7474
47
47744
7744
474747447444474

Output:
3
2
5
4
11

Explanation

In the first test case all different swap permutations of S = 7474 are 4747, 4774, 7447, 7474, 7744. Corresponding values of F(T) are 3, 3, 3, 2, 2. Hence the answer is 3.

In the second, third and fourth test cases there exists a non-decreasing swap permutation and hence the answer is equal to the length of the string. Namely, S = 47 is itself non-decreasing, for S = 47744 we can obtain the non-decreasing T = 44477 if we delete the substring S[2,3] = 77 and then insert it back into the end of the string, finally, for S = 7744 we can obtain the non-decreasing T = 4477 if we delete the substring S[1,2] = 77 and then insert it back into the end of the string.


Author: witua
Tester: anton_lunyov
Editorial http://discuss.codechef.com/problems/LUCKYSWP
Date Added: 1-03-2012
Time Limit: 2 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.

F("4747") in example 1 should

orderof1 @ 20 May 2012 10:45 PM
F("4747") in example 1 should be 2 right?

@orderof1 Not it is 3 since

anton_adm @ 20 May 2012 10:51 PM
@orderof1 Not it is 3 since "447" is its non-decreasing subsequence.

Is the 5th test correct?

snich @ 20 May 2012 11:18 PM
Is the 5th test correct?

@snich Yes.

anton_adm @ 20 May 2012 11:35 PM
@snich Yes.

could you explain how the 5th

somay_jain16 @ 20 May 2012 11:42 PM
could you explain how the 5th test case is true?

@somay_jain16 We have 10

anton_adm @ 21 May 2012 12:00 AM
@somay_jain16 We have 10 digits 4 in the string. So deleting of any 7 and inserting it back into the end of string we obtain the string with non-decreasing subsequence 44444444447 of the length 11. It is hard to explain why 12 is not reachable you should write all different swap permutations and check them.

submission was blocked last 3

bcurcio @ 21 May 2012 12:09 AM
submission was blocked last 3 minutes

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.