hpuoj 1673: Problem K Lexicographically Largest Substring

来源:互联网 发布:矩阵 方阵 编辑:程序博客网 时间:2024/05/16 16:55

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.

样例输入

2dcbadcbbdabdasd

样例输出

dcbbdsd
暴力:
#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
原创粉丝点击