首页 > 编程语言 > 详细

2010上交:后缀子串排序

时间:2016-04-09 23:27:54      阅读:382      评论:0      收藏:0      [点我收藏+]
题目描述:

对于一个字符串,将其后缀子串进行排序,例如grain
其子串有:
grain 
rain 
ain 
in 
n

然后对各子串按字典顺序排序,即: 
ain,grain,in,n,rain

输入:

每个案例为一行字符串。

输出:

将子串排序输出

样例输入:
grain
样例输出:
ain
grain
in
n
rain

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN=10005;
char s[MAXN];
int order[MAXN];
int len;
bool comp(int x,int y)
{
    while(x<len&&y<len)
    {
        if(s[x]==s[y])
        {
            x++;
            y++;
        }
        else
        {
            return s[x]<s[y];
        }
    }
    return x>y;
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        len=strlen(s);
        for(int i=0;i<len;i++)
            order[i]=i;
        sort(order,order+len,comp);
        for(int i=0;i<len;i++)
        {
            for(int j=order[i];j<len;j++)
            {
                printf("%c",s[j]);
            }
            printf("\n");
        }
    }
    return 0;
}

 

2010上交:后缀子串排序

原文:http://www.cnblogs.com/program-ccc/p/5372947.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!