afterward

导航

 

 

还是看大神的代码把!

View Code
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 200010;

char s[MAXN];
int next[MAXN];//next 指向的是以此点开始的字符串应该与原串中的哪个字符进行比较
// 当然比较的时候此串也要找对应的字符进行比较 如果超过字符串长度 说明相等

void getNext()
{
    int i, j;
    int l = 1, r = -1;//l 和 r 代表循环串的左右位置
    next[0] = 0;
    for(i=1;s[i];++i)
    {
        //cout<<"s[i]:"<<s[i]<<endl;
        if(i + next[i - l] - 1 < r)
        {
            next[i] = next[i - l];
            cout<<"next["<<i<<"]:"<<next[i]<<endl;
//如果前面对应的循环串对应位置字符要找的位置对应到本串中字符要找的位置没有超过r  说明可以直接将答案copy过来
        }
        else
        {
            j = max(r - i + 1, 0);
            for(;s[i+j] && s[i+j]==s[j];++j);
            next[i] = j;//否则选择可以最靠后开始的位置进行依次比较 直到找到不同的 并记录循环串位置
            cout<<"----next["<<i<<"]:"<<next[i]<<endl;
            l = i;
            r = i + j - 1;
            cout<<l<<" "<<r<<endl;
        }
    }
    next[0] = i;
}

int main()
{
    int caseNumber;
    scanf("%d", &caseNumber);
    for(int cas=1;cas<=caseNumber;++cas)
    {
        scanf("%s", s);
        int len = strlen(s);
        for(int i=0;i<len;++i)
        {
            s[i + len] = s[i];
        }
        s[len<<1] = 0;
        getNext();
        int ml, k;
        for(k = 1;k < len && k + next[k] < len;++k);
        ml = len % k ? len : k;
        //cout<<ml<<endl;
        int l = 0, e = 0, g = 0;
        for(int i=0;i<ml;++i)
        {
            if(next[i] >= len)
            {
                ++ e;
            }
            else if(s[i + next[i]] < s[next[i]])
            {
                ++ l;
            }
            else
            {
                ++ g;
            }
        }
        printf("Case %d: %d %d %d\n", cas, l, e, g);
    }
    return 0;
}

 

本来看着不难,好不容易没一点错误做出来了还是超时!超时!每次都这样,算法就是不止要做出来还要想办法节约时间啊。

View Code
#include<iostream>
#include<string.h>
#include<stdio.h>
#include <algorithm>
using namespace std;
char a[200010],b[100005];
int main()
{
    int x,length;
    char *p,*k;
    int L,E,G,i;
    cin>>x;
    int d=0;
    while( x--)
    {
        L=G=0;
        E=1;
        cin>>a;
        length=strlen(a);
        strcpy(b,a);
        strcat(a,b);
        a[2*length]='\0';

        p=a+1;
        while(*p!='\0')
        {
            i=0;
            if(*p>a[i]) G++;
            else if(*p<a[i]) L++;
            else if(*p==a[i])
            {
                k=p+1;
                while(*k==a[i+1]&&*k!='\0')
                {
                    *k++;
                    i++;
                }
                 //cout<<"*k..."<<*k<<"a["<<i<<"]:"<<a[i]<<endl;
                if(*k=='\0')
                {
                    E++;
                    break;
                }
                else
                {
                    if(*k>a[i+1]) G++;
                    if(*k<a[i+1]) L++;
                }
            }
            *p++;
        }

        cout<<"Case "<<++d<<": "<<L<<" "<<1<< " "<<G<<" "<<endl;

    }

    return 0;
}

 

Revolving Digits

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 583    Accepted Submission(s): 155


Problem Description
One day Silence is interested in revolving the digits of a positive integer. In the revolving operation, he can put several last digits to the front of the integer. Of course, he can put all the digits to the front, so he will get the integer itself. For example, he can change 123 into 312, 231 and 123. Now he wanted to know how many different integers he can get that is less than the original integer, how many different integers he can get that is equal to the original integer and how many different integers he can get that is greater than the original integer. We will ensure that the original integer is positive and it has no leading zeros, but if we get an integer with some leading zeros by revolving the digits, we will regard the new integer as it has no leading zeros. For example, if the original integer is 104, we can get 410, 41 and 104.
 

Input
The first line of the input contains an integer T (1<=T<=50) which means the number of test cases.
For each test cases, there is only one line that is the original integer N. we will ensure that N is an positive integer without leading zeros and N is less than 10^100000.
 

Output
For each test case, please output a line which is "Case X: L E G", X means the number of the test case. And L means the number of integers is less than N that we can get by revolving digits. E means the number of integers is equal to N. G means the number of integers is greater than N.
 

Sample Input
1
341
 

Sample Output
Case 1: 1 1 1
 

posted on 2012-08-13 16:38  afterward  阅读(269)  评论(0编辑  收藏  举报