求最大连续递增数字串(如“ads3sl456789DF3456ld345AA”中的“456789”)
一个相关题:找到一个字符串中的一个连续子串,这个子串内不能有任何两个字符是相同的,并且这个子串是符合要求的最长的。例如输入"abcbef",输出"cbef"。(代码弄丢,只有一个输出长的的版本,时间有限不补了)
比较简单。做第二个的时候因为一下午和编程之美较劲,脑袋蒙了,搞不定。还是看了一眼答案才出来。失败。
- /*
- * =====================================================================================
- *
- * Filename: maxincrease.c
- *
- * Description:
- *
- * Version: 1.0
- * Created: 10/22/2012 09:54:26 PM
- * Revision: none
- * Compiler: gcc
- *
- * Author: royn.wang.renyuan@gmail.com (),
- * Organization:
- *
- * =====================================================================================
- */
- #include <stdlib.h>
- #include <stdio.h>
- #define MAX 10000
- /*
- * === FUNCTION ======================================================================
- * Name: main
- * Description:
- * =====================================================================================
- */
- int
- main ( int argc, char *argv[] )
- {
- char input[] = "ads3sl456789DF3456ld345AA";
- int max = 0;
- int i = 0;
- int result[MAX] = {0};
- int last = -1;
- int endindex = -1;
- result[0] = 0;
- for(;i<sizeof(input)-1;i++){
- if(input[i] >='0' && input[i]<='9' && input[i] - '0' >last ){
- last = input[i] - '0';
- result[i] = result[i-1] + 1;
- if( max<result[i]){
- max = result[i];
- endindex = i;
- }
- }
- else{
- last = -1;
- result[i] = 0;
- }
- }
- printf ( "max length is %d\n",max );
- for(i= endindex - max+1;i<=endindex;i++){
- printf ( "%c", input[i] );
- }
- printf ( "\n" );
- return EXIT_SUCCESS;
- } /* ---------- end of function main ---------- */
- #include <stdlib.h>
- #include <stdio.h>
- #define MAX 100
- int main(){
- char input[] = "abcbedf";
- int index[26];
- int length[MAX] = {0};
- length[0] = 1;
- int max = 0;
- int i;
- for(i = 0; i<26;i++){
- index[i] = -1;
- }
- for(i = 1;i<sizeof(input)-1;i++){
- if(index[input[i] - 'a'] == -1){
- length[i] = length[i-1]+1;
- }
- else{
- length[i] = i-index[input[i]-'a'];
- }
- index[input[i] - 'a'] = i;
- max = max>length[i] ? max:length[i];
- }
- printf("%d\n", max);
- }
阅读(461) | 评论(0) | 转发(0) |