hpuoj 1673: Problem K Lexicographically Largest Substring

1673: Problem K Lexicographically Largest Substring

时间限制: 1 Sec   内存限制: 128 MB
提交: 8   解决: 7
[ 提交][ 状态][ 讨论版]

题目描述

 (Standard Input / Standard Output)
Given a string, you should find the lexicographically largest substring. S is lexicographically larger than T if and only if T is the prefix of S, or there is an i satisfying that S[j] = T[j] for every j < i and S[i] > T[i]. For example, string ‘abc’ is lexicographically larger than ‘abbd’ and less than ‘abcd’.



输入

The first line of input contains an integer T (T <= 20) representing the number of test cases.
Each test case contains only a string in one line, which length is less than 100.

输出

For each test case, output the lexicographically largest substring of the given string.

样例输入

2
dcbadcbbd
abdasd

样例输出

dcbbd
sd
暴力:
#include<stdio.h> 
#include<string.h> 
#include<math.h> 
#include<stdlib.h> 
#include<queue> 
#include<stack> 
#include<algorithm> 
using namespace std; 
struct record 
{ 
    char s[110]; 
}num[10100]; 
char a[10100][110]; 
bool cmp(record a,record b) 
{ 
    return a.s>b.s;  
} 
int main() 
{ 
    int i,j,l; 
    int t,k; 
    int p,len; 
    char str[110]; 
    char b[110]; 
    scanf("%d",&t); 
    while(t--) 
    { 
        scanf("%s",str); 
        l=strlen(str); 
        p=0; 
        memset(num,'\0',sizeof(num)); 
        for(i=0;i<l;i++)//开头  
        { 
            for(j=i;j<l;j++)//结尾  
            { 
                len=0; 
                for(k=i;k<=j;k++) 
                { 
                    num[p].s[len++]=str[k]; 
                }  
                p++; 
            } 
        } 
        strcpy(b,num[0].s); 
        for(i=1;i<p;i++) 
        { 
            if(strcmp(num[i].s,b)>0&&strcmp(num[i].s,str)!=0) 
            strcpy(b,num[i].s); 
        } 
        printf("%s\n",b); 
        //sort(num,num+p,cmp); 
        //printf("%s\n",num[p-1].s); 
    } 
    return 0; 
} 
 
 
sort:
#include<stdio.h> 
#include<string.h> 
#include<math.h> 
#include<stdlib.h> 
#include<queue> 
#include<stack> 
#include<algorithm> 
using namespace std; 
struct record 
{ 
    char s[110]; 
}num[10100]; 
bool cmp(record a,record b) 
{ 
    return strcmp(a.s,b.s)>0;  
} 
int main() 
{ 
    int i,j,l; 
    int t,k; 
    int p,len; 
    char str[110]; 
    char b[110]; 
    scanf("%d",&t); 
    while(t--) 
    { 
        scanf("%s",str); 
        l=strlen(str); 
        p=0; 
        memset(num,'\0',sizeof(num)); 
        for(i=0;i<l;i++)//开头  
        { 
            for(j=i;j<l;j++)//结尾  
            { 
                len=0; 
                for(k=i;k<=j;k++) 
                { 
                    num[p].s[len++]=str[k]; 
                }  
                p++; 
            } 
        } 
        sort(num,num+p,cmp); 
        if(strcmp(num[0].s,str)) 
        printf("%s\n",num[0].s); 
        else
        printf("%s\n",num[1].s); 
    } 
    return 0; 
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值