算法竞赛入门经典第五章第一节例题实现

#include<iostream>
#include<cstring>
using namespace std;

#define MAX 1000

//5.1.1 WERTYU
//用常量表简化代码
/*
char Str[MAX];
char Table[100] = {
    '`','1','2','3','4','5','6','7','8','9','0','-','=',127,
    '\t','Q','W','E','R','T','Y','U','I','O','P','[',']','\\',
    'A','S','D','F','G','H','J','K','L',';','\'',13,
    'Z','X','C','V','B','N','M',',','.','/',
    ' ','\0',
};*/

//5.1.3 周期串
char CharArray[MAX];

int main()
{
    //5.1.1 WERTYU
    /*
    gets(Str);
    int count = strlen(Str);
    for (int i = 0; i < count; i++)
    {
        char ch = Str[i];
        if (ch == 32)
            cout << ' ';
        else
            cout << *(strchr(Table,ch)-1);
    }
    cout << endl;*/

    //5.1.2 Tex括号
    //非标记位版本
    /*
    int count = 0;
    char ch;
    while ((ch = getchar())!= EOF)
        str[count++] = ch;
    str[count] = '\0';
    
    int i = 0;
    for (; i < count; i++)
    {
        if (str[i] == 34)
        {
            int j;
            for (j = i + 1; j < count; j++)
            {
                if (str[j] == 34)
                {
                    cout << "``";
                    for (int k = i+1; k < j; k++)
                    {
                        cout << str[k];
                    }
                    cout << "\"";
                    break;
                }
            }
            //不要忘记这时候的j已经是加1后的,所以不是i = j + 1
            i = j;
        }
        else
            cout << str[i];
    }*/

    //标记位版本
    /*
    char c;
    int q = 1;
    while ((c = getchar())!= EOF)
    {
        if (c == 34)
        {
            printf("%s",q ? "``" :"\"");
            q = !q;
        }
        else
            printf("%c",c);
    }*/

    //5.1.3 周期串
    //遇到换行符停止,并将换行符转化成'\0'
    gets(CharArray);
    int count = strlen(CharArray);
    cout << count << endl;
    for (int i = 1; i <= count; i++)
    {
        if (count % i == 0)
        {
            cout << " i = " << i << endl;
            int Period = i;
            int if_end = -1;
            for (int j = 0; j < Period; j++)
            {
                int temp = Period;
                int n = j;
                //完成一次判断
                while (n < count)
                {
                    if (CharArray[n] == CharArray[n+Period])
                    {
                        cout << " n = " << n << endl;
                        n += Period;
                        if ((n + Period) >= count)
                            break;
                    }
                    else
                        break;
                }

                if (n + Period < count)
                {
                    cout << "不符合条件" << endl;
                    break;
                }
                else
                {
                    if_end = n;
                    cout << "****" << if_end << endl;
                    continue;
                }
            }
            if (if_end == count -1)
            {
                cout << Period << endl;
                break;
            }
        }
        else
            continue;
    }

    system("pause");
    return 0;
}


你可能感兴趣的:(算法竞赛入门经典第五章第一节例题实现)