Ultraviolet A 10132-file fragmentation

Source: Internet
Author: User

I haven't handed in questions for a long time, and I found that the recent bad status is not good, and the AC rate is disgusting ~

This questionCodeA little long, but the actual idea is very simple. qsort takes a look at the order and then matches the longest string with the shortest string, because a string can be divided into two different longest strings at most, therefore, the longest match with the shortest substring is a maximum of eight cases. One of them is the answer.

The Code is as follows:

# Include <iostream> # include <cstring> # include <cstdlib> # include <cstdio> using namespace STD; int CMP (const void * a, const void * B) {int size = strlen (char *) A)-strlen (char *) B); If (! Size) return strcmp (char *) A, (char *) B); Return size;} int main () {# ifdef st freopen ("sample.txt", "r ", stdin); # endif char a [150] [260], B [260], C [260]; int num; scanf ("% d", & num ); getchar (); While (Num --) {int Ct = 0; memset (A, 0, sizeof (a); While (1) {gets (A [cT]); If (! Strlen (A [cT]) break; ++ CT;} qsort (A, CT, sizeof (A [0]), CMP); int flag = 0; if (strlen (A [0]) = strlen (A [1]) {sprintf (B, "% S % s", a [0], A [CT-1]); sprintf (C, "% S % s", a [CT-2], a [1]); If (! Strcmp (B, c) // if the two strings are the same, the request is complete string flag = 1; if (! Flag) {sprintf (B, "% S % s", a [1], a [CT-1]); sprintf (C, "% S % s ", A [CT-2], a [0]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [0]); sprintf (C, "% S % s ", A [1], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [1]); sprintf (C, "% S % s ", A [0], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [0], a [CT-1]); sprintf (C, "% S % s ", A [1], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [1], a [CT-1]); sprintf (C, "% S % s ", A [0], a [CT-2]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [0]); sprintf (C, "% S % s ", A [CT-2], a [1]); If (! Strcmp (B, c) Flag = 1;} If (! Flag) {sprintf (B, "% S % s", a [CT-1], a [1]); sprintf (C, "% S % s ", A [CT-2], a [0]); If (! Strcmp (B, c) Flag = 1 ;}} else sprintf (B, "% S % s", a [CT-1], a [0]); printf ("% s \ n", B); If (Num) puts ("");} return 0 ;}

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.