junior19

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1.有1,2,....一直到n的无序数组,求排序算法,并且要求时间复杂度为O(n),空间复杂度O(1),使用交换,而且一次只能交换两个数。

# include <stdio.h>
int main()
{
    int a[10] = {8,7,5,4,1,6,3,9,10,2};
    for(int i=0; i<10; ++i)
        while(a[i] != i+1)
        {
            int t = a[i];
            a[i] = a[t-1];
            a[t-1] = t;
        }
    for(int i=0; i<10; ++i)
        printf("%d ",a[i]);
    return 0;
}

2.求一个字符串的最大回文前缀长度。回文是指正反方向读起来都一样的字符串,比如“abcdcba”就是一个回文。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# define ull unsigned long long
using namespace std;
char s[1000000];
int main()//Hash
{
    while(~scanf("%s",s))
    {
        int len = strlen(s);
        ull a=0, b=0, pow=1, seed=233, ans=0;
        for(int i=0; i<len; ++i)
        {
            a = a + s[i]*pow;
            pow *= seed;
            b = b*seed + s[i];
            if(a == b)
                ans = max(ans, (ull)i+1);
        }
        printf("%llu\n",ans);
    }
    return 0;
}

posted on 2017-03-28 23:30  junior19  阅读(99)  评论(0编辑  收藏  举报