uva1584_水题(环状串的处理)


///
作者:tt2767
声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0
查看本文更新与讨论请点击:http://blog.csdn.net/tt2767
链接被删请百度: CSDN tt2767
///


我的方法是:用指针不断从头部向尾部转移值;
大白代码仓库中方法:利用模运算;

My Code:



#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
//
#include<iostream>
#include<algorithm>
#include<string>
#include <iterator>
#include<sstream>
#include<functional>
#include<numeric>
///
#include<vector>
#include<map>
#include <stack>
#include<queue>
#include<set>
#include <bitset>
#include <list>
using namespace std;
#define lch(x)  ((x) << 1)
#define rch(x) ((x)<<1|1)
#define dad(x) ((x)>>1)
#define lowbit(x) ((x)&(-x))
typedef  long long int LL;
const int INF = ~0U>>1;
const double eps = 1e-6;
const long double PI = acos(0.0) * 2.0;
const int N = 10 + 1000;
char s[N],ans[N];
bool  cmp(char *x ,char *y ,int l); //按字典序比较
int main()
{
    //ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
#else
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    int Case;

    scanf("%d%*c",&Case);
    while(Case--)
    {
        memset(ans,'\0',sizeof(ans));
        memset(s,'\0',sizeof(s));
        char *p = s;
        gets(p);
        int l = strlen(p);
        memcpy(ans,p,sizeof(char)*l);
        int num = l;

        while(num--)
        {
            *(p+l) = *(p++);//不断向尾部传值
            if( cmp(ans,p,l) )    memcpy(ans,p,sizeof(char)*l);

        }
        puts(ans);
    }
    return 0;
}

bool  cmp(char *x ,char *y ,int l)
{
    for(int i = 0 ; i < l; i ++)
    {
        if(y[i] < x[i])    return 1;
        else if(y[i]>x[i])  return 0;
    }
    return 0;
}

代码仓库:

// UVa1584(LA3225) Circular Sequence
// Rujia Liu
#include<stdio.h>
#include<string.h>
#define maxn 105

// 环状串s的表示法p是否比表示法q的字典序小
int less(const char * s, int p, int q)
{
    int n = strlen(s);
    for (int i = 0; i < n; i++)
        if (s[(p+i)%n] != s[(q+i)%n])
            return s[(p+i)%n] < s[(q+i)%n];
    return 0; // 相等
}

int main()
{
    int T;
    char s[maxn];
    scanf("%d", &T);
    while (T--)
    {
        scanf("%s", s);
        int ans = 0;
        int n = strlen(s);
        for (int i = 1; i < n; i++)
            if (less(s, i, ans)) ans = i;
        for (int i = 0; i < n; i++)
            putchar(s[(i+ans)%n]);
        putchar('\n');
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值