关闭

百度账号选取方案

标签: 算法
120人阅读 评论(0) 收藏 举报
分类:

计蒜客上的一道题百度账号选取方案
我理解的意思是从字符串中取两个不相交的子串,如果两个子串的冲突值相等,则选取它。
那么幸运值是怎么算的?比如aab和baa的幸运值一样吗,都是多少??

下面是代码:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 1000

using namespace std;


int calNum(char* str)
{
    int p = 0, p_max = 0, i = 0, j = 0, flag = 0, flag_end = 0, counts = 0;
    char s[N], s1[N];
    int len = strlen(str);
    int len1 = 0;
    //char c;
    s1[0] = 0;
    s[0] = 0;
    for(i = 0; i < len/2; i++)
    {
        p = 0;
        flag = 0;
        char str_temp1[N] = {str[i]};
        strcat(s, str_temp1);
        //cout << "s=" << s << endl;
        while(flag == 0)
        {
            len1 = strlen(s);
            s1[0] = 0;
            if(len1 * (p + 2) > len)
            {
                break;
            }
            for(j = len1 * (p + 1); j < len1 + (p + 1) * len1 && j < len; j++)
            {
                char str_temp2[N] = {str[j]};
                strcat(s1, str_temp2);
            }
            if(strcmp(s1, s) == 0){
                p++;
                flag = 0;
                if(j == len)
                {
                    if(p_max < p)
                    {
                        p_max = p;
                    }
                    break;
                }
            }else{
                if(j == len)
                {
                    flag_end = 1;
                    p = 0;
                }
                flag = 1;
            }
            if(p == 0)
            {
                flag = 1;
            }

        }
        if(flag_end == 1)
        {
            break;
        }

    }
    return p_max;
}


int main()
{

    //while(true)
    //{
    /*char str[100];
    cin >> str;
    int p = 0;
    p = calNum(str);
    cout << "p=" << p << endl;*/
        char sub_str1[N], sub_str2[N];
        char str[N];

        int i=0,j=0,k=0,h=0,n=0,len=0,p1=0,p2=0,counts=0;

        cin >> str;
        len = strlen(str);
        n = len;


        for(i = 0; i < n; i++)
        {
            sub_str1[0] = 0;
            for(j = i; j < n; j++)
            {
                char s1[N] = {str[j]};
                strcat(sub_str1, s1);
                p1 = calNum(sub_str1);
                for(k = j + 1; k < n; k++)
                {
                    sub_str2[0] = 0;

                    for(h = k; h < n; h++)
                    {
                        char s2[N] = {str[h]};
                        strcat(sub_str2, s2);
                        p2 = calNum(sub_str2);
                        //cout << "p1=" << p1 << ",p2=" << p2 << "(sub_str1=" << sub_str1 << ",sub_str2=" << sub_str2 << ")"<< endl;
                        if(p1 == p2){
                            counts++;
                        }
                    }

                }
            }

        }
        cout << counts << endl;
    //}

    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29214次
    • 积分:997
    • 等级:
    • 排名:千里之外
    • 原创:74篇
    • 转载:3篇
    • 译文:0篇
    • 评论:3条
    最新评论