HDU-3556-Continued Fraction

Source: Internet
Author: User

Start with the question:

Continued Fraction

Time Limit: 2000/1000 MS (Java/others) memory limit: 131072/65536 K (Java/Others)
Total submission (s): 332 accepted submission (s): 106


Problem descriptiondumbear loves numbers very much.
One day dumbear found that each number can be expressed as a continued fraction. See below.

Formally, we say a number K can be expressed as a continued faction if

Where A0, A1 ,..., An are positive integers should t that A0 maybe be 0 and an cannot be 1.
Dumbear also found a sequence which looks like the farey sequence. initially the sequenceand if we insert an elementbetween all the two adjacent element, in Di, then we get a sequence di + 1. so you can seeandassume initially you are on the elementin D0, And if now you are on the element K in Di, then if you go left ('l ') (or right ('R') You will be on the left (or right) element of K in di + 1. so a sequence composed of 'l' and 'R' denotes a number. such as 'rl 'denotes the number

Now give you a sequence composed of 'l' and 'R', you shoshould print the continued fraction form of the number. you shoshould use '-' to show the vinculum (the horizontal line), You shoshould print one space both in front and back of '+ ', and all parts up or down the vinculum shocould be right aligned. you shoshould not print unnecessary space, '-' or other character. see details in sample.

 

Inputthere are several test cases in the input.
For each test case, there is a single line contains only a sequence composed of 'l' and 'R'. The length of the sequence will not exceed 10000.
The input terminates by end of File marker.

 

Outputfor each test case, output the continued fraction form of the number which the input sequence denotes. The total amount of output will not exceed 4 MB.

 

Sample inputlrrl

 

Sample output 10 + ----- 1 1 +-2 11 +-2 meaning: A string with only L and R is given, which is moved according to its definition, then output the final result according to the fraction given by him. Simulate + find the rule. First, simulate its movement according to its definition, and then you can find out the number of each entry in the [] Array Based on your own analysis, and then output it in the format. Here, we can find out how to find the [] array, and then we can get the number of spaces to be output through the length relationship between the front and back items, however, in this case, direct submission may result in WA. Here, you can find the rule by outputting the first few items. Set l, ll, LR, lll, LLR, lrl, LRR, R, rl, RR, rll, rlr, rrl, rrr (that is, when there is a change in the previous items) output, and then through observation, we can find that for the first item, if the current item is equal to the previous one, then a [tot] ++, otherwise a [tot] --; Tot ++; A [tot] = 2; in fact, if you write well, you can include the previous information in the formula. Then directly construct a [], which guarantees the printing speed and accuracy. Code:
 1 #include <cstdio> 2 #include <cstring> 3 #include <string> 4 #include <algorithm> 5 #define MAX 10002 6 #define PUTS(M,x) for(int k=0;k<M;k++) putchar(x) 7 #define ll long long 8 using namespace std; 9 10 char c[MAX];11 int l;12 ll a[MAX];13 char ss[MAX<<4][20];14 int len[MAX];15 int tot;16 typedef struct{17     ll fz,fm;18 }fs;19 20 fs A[3],p;21 22 void cons(int l){23     if(c[0]==‘L‘){24         a[0]=0; a[1]=2; tot=1;25     }else{26         a[0]=2; tot=0;27     }28     for(int i=1;i<l;i++){29         if(c[i]==c[i-1]) a[tot]++;30         else{31             a[tot]--;32             a[++tot]=2;33         }34     }35     tot++;36 }37 38 int main()39 {40     int M;41     //freopen("data.txt","r",stdin);42     while(scanf("%s",c)!=EOF){43         l=strlen(c);44 //        A[0].fz=0;  A[0].fm=1;45 //        A[1].fz=1;  A[1].fm=1;46 //        A[2].fz=1;  A[2].fm=0;47 //        for(int i=0;i<l;i++){48 //            if(c[i]==‘L‘){49 //                A[2]=A[1];50 //            }else{51 //                A[0]=A[1];52 //            }53 //            A[1].fz=A[0].fz+A[2].fz;54 //            A[1].fm=A[0].fm+A[2].fm;55 //        }56 //        tot=0;57 //        p=A[1];58 //        while(1){59 //            a[tot]=p.fz/p.fm;60 //            sprintf(ss[tot],"%I64d",a[tot]);61 //            tot++;62 //            p.fz=p.fz%p.fm;63 //            if(p.fz==0) break;64 //            else if(p.fz==1){65 //                a[tot]=p.fm;66 //                sprintf(ss[tot],"%I64d",a[tot]);67 //                tot++;68 //                break;69 //            }70 //            swap(p.fz,p.fm);71 //        }72         cons(l);73         for(int i=0;i<tot;i++) sprintf(ss[i],"%I64d",a[i]);74         len[tot-1]=strlen(ss[tot-1]);75         len[tot-2]=strlen(ss[tot-2]) + 3 + strlen(ss[tot-1]);76         for(int i=tot-3;i>=0;i--){77             len[i]=strlen(ss[i]) + 3 + len[i+1];78         }79 80 //        for(int i=0;i<tot;i++) printf("%I64d ",a[i]);81 //        printf("\n");82 //        for(int i=0;i<tot;i++) printf("%d ",len[i]);83 //        printf("\n");84         M=len[0];85         for(int i=0;i<tot-1;i++){86             PUTS(M-1,‘ ‘);  putchar(‘1‘);   putchar(‘\n‘);87             PUTS(M-len[i],‘ ‘);88             printf("%s + ",ss[i]);89             PUTS(len[i+1],‘-‘);90             putchar(‘\n‘);91         }92         PUTS(M-(int)strlen(ss[tot-1]),‘ ‘);93         printf("%s",ss[tot-1]);94         printf("\n");95     }96     return 0;97 }
/X 3556 */

 

 

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.