timus1558 最短循环节

                                    #define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 2000000000
#define EPS 1e-6
typedef long long LL;
const double PI = acos(-1.0);
inline int sgn(double x){return fabs(x) < EPS ? 0 :(x < 0 ? -1 : 1);}
int NEXT[100];
char t[200];
void Getnext(){
    int j=0,k=-1;
    int len=strlen(t);
    NEXT[j]=k;
    while(j<len){
        if(k==-1||t[k]==t[j]){
            j++;k++;NEXT[j]=k;
        }
        else k=NEXT[k];
    }
}
int main()
{    
    #ifdef DeBUGs
        freopen("//home//amb//桌面//1.in","r",stdin);
    #endif
        char num1[20];
        char num2[20];
        while(scanf("%s%s",num1,num2)+1)
        {
            int i,j,k;
            char num3[20]=zero;
            char old[20]=zero;
            int len=strlen(num1);
            int flag=0;
            memset(t,0,sizeof(t));
            k=0;
            for(i=len-2;i>=1;i--)
            {
                num3[i-1]=(num1[i]+num2[i]-2*'0'+k)%10+'0';
                k=(num1[i]+num2[i]-2*'0'+k)/10;
            }
            strcat(old,num3);
            if(k)
            {
                char ad;
                for(i=strlen(num3)-1;i>=0;i--)
                {
                    ad=num3[i];
                    num3[i]=(num3[i]+k-'0')%10+'0';
                    k=(ad+k-'0')/10;
                }
            }
            if(strcmp(old,num3)!=0)
                flag=1;
            strcat(t,num3);
            Getnext();
            len-=2;
            char ss[100]=zero;
            int dd=0;
            int len2=len%(len-NEXT[len])==0?len-NEXT[len]:len;
           // cout<<flag2<<endl;
            if(flag&&len!=1)
            {
                for(i=0;i<len2;i++)
                {
                   // printf("%c",num3[i]);
                    ss[dd++]=num3[i];
                }
                char sss[500]=zero;
                strcpy(sss,ss);
                strcat(sss,ss);
                 strcat(sss,ss);
                strcat(sss,old);
                for(int l=2;l<=9;l++)//baoliuweizhicong2kaishisou
                {
                    k=0;
                    for(i=strlen(sss)-l;i>=0;i--,k++)
                    {
                        if(k==dd)
                            {
                                printf("(");
                                for(i=strlen(sss)-l+1-dd;i<strlen(sss)-l+1;i++)
                                {
                                    printf("%c",sss[i]);
                                }
                                printf(")");
                                for(j=i;j<strlen(sss);j++)
                                printf("%c",sss[j]);
                                printf("
");
                                break;
                            }
                                if(sss[i]!=sss[i-dd])
                                    {
                                        flag2=0;
                                        break;
                                    }
                    }
                    if(k==dd)
                        break;
               }
                if(k==dd)
                    continue;
            }
                  dd=0;
                printf("(");
                for(i=0;i<len2;i++)
                {
                    printf("%c",num3[i]);
                    ss[dd++]=num3[i];
                }
                printf(")");
                if(flag)
                {
                    for(i=0;i<len;)
                    {
                        for(j=0;j<dd;j++,i++)
                        {
                            if(old[i]==ss[j])
                                continue;
                            else
                                break;
                        }
                        if(j!=dd)
                        {
                            i-=j;
                            break;
                        }
                    }
                    for(;i<len;i++)
                    {
                       printf("%c", old[i]);
                    }

            }
            printf("
");
        }
        
    return 0;
}
View Code

附上测试数据

(234)
(342)
(936)
(174)
(901097)
(222025)
(436)
(564)
(789)
(789)
(1)
(9)
(413)
(926)
(1)
(8)
(567)
(765)
(123456789)
(987654321)
(001)
(100)
(544444444)
(555555555)
(13681872)
(95650177)

///////////////////////////
(576)
(1)0
(312)2
(100)0
(957)8
(1)0
(403)39
(9)
(3)2
(1)0
(101)
(100000000)099999999
(50093320)49
View Code

1558. Periodical Numbers

Time limit: 1.0 second
Memory limit: 64 MB
Little Tom likes amusing mathematical tasks a lot. After studying ordinary periodical numbers he wondered, what if period will be before decimal point. Generally speaking, such a "number" will have infinite number of digits before decimal point, and it will not be even a number, but it is possible to apply some operations to them. But after trying to sum up this numbers for a while, he found this task a bit complicated, even when numbers have periods of the same length and don't have unperiodical part. So he decided to write a program that will solve this problem. But he is not very good at programming, so asked you to help him and write it.
Periodical numbers can be written in form (a1a2ak)b1b2bm = …a1a2ak a1a2ak a1a2ak b1b2bm, where ai and bjare digits. The summation process starts from the less significant digit and going on like in addition of normal numbers, but never finishes. Your task is to sum up two periodical infinite numbers.

Input

Input has two lines containing two infinite periodical numbers each. It is guaranteed that the given numbers will not have unperiodical part (i.e. will be given in form "(a1a2 … ak)") and the given periods of numbers will be the same length not greater than nine.

Output

The output must have one line containing desired number itself. It must be printed in the representation with minimal period length. Among such representations the one having the least unperiodical part's length must be chosen.

Sample

inputoutput
(234)
(342)
(576)
Problem Source: Novosibirsk SU Contest. Petrozavodsk training camp, September 2007
原文地址:https://www.cnblogs.com/Skyxj/p/3367245.html