1 条题解

  • 0
    @ 2024-12-24 10:06:06

    C :

    #include<stdio.h>
    #include<string.h>
    
    int main(){
        char str[100];
        int i, count, len,heap;
        while(scanf("%s", str)==1){
            count=0;
            heap=0;
            len=strlen(str);
            for(i=0; i<len; i++){
                if(str[i]=='D'){
                    if(count>0){
                        printf("%d", count+1);
                        heap++;
                        count=0;
                        break;
                    }
                }
                else{
                     count++;
                }
            }
            i++;
            for(; i<len; i++){
                if(str[i]=='D'){
                    if(count>0){
                        printf(" %d", count+1);
                        heap++;
                        count=0;
                    }
                }
                else{
                     count++;
                }
            }
            if(heap==0) printf("0\n"); 
            else printf("\n");        
        }
        return 0;
    }
    
    

    C++ :

    #include <cstdio>
    #include <stack>
    using namespace std;
    
    int main() {
    	int i, flag, k;
    	char s[51];
    	while (scanf("%s", s) != EOF) {
    		stack<char> S;
    		for (flag = k = i = 0; s[i]; i++) {
    			if (s[i] == 'D') {
    				if (!S.empty()) {
    					if (flag)
    						printf(" %d", S.size() + 1);
    					else {
    						printf("%d", S.size() + 1);
    						flag = 1;
    					}
    					k = 1;
    				}
    				while (!S.empty())
    					S.pop();
    			} else
    				S.push(s[i]);
    		}
    		if (!k)
    			printf("0");
    		printf("\n");
    	}
    	return 0;
    }
    

    Java :

    import java.io.*;
    import java.util.*;
    import java.util.Arrays;
    
    public class Main{
      public static void main(String [] args){
        final int NUM = 51;
        String  s;
        int i, j;
        Scanner in = new Scanner(System.in); 
        while(in.hasNext()){
          s = in.next();
          int[][]result = new int[NUM][2];
          for(i=0; i<NUM; i++){
            result[i][0] = 0;
            result[i][1] = 0;
          }
    
          int statics = 0;
          int length = s.length();
    
          for(i=0; i<length; i++){
            if(s.charAt(i) != 'D'){
              result[statics][0]++;
            }
            else{
              if(result[statics][0] > 0)
                result[statics][1] = 1; 
              else{
                result[statics][1] = 0;
                continue;
              }
              statics++;
            }
          }
    
          i=0; 
          while(result[i][0] != 0){
            
            if(result[i][1] == 0){
              i++;
              continue;
            }
            if(i != 0)
              System.out.print(" ");
            System.out.print(result[i][0]+1);
            
            i++;
          }
          if(result[0][1] == 0)
            System.out.print("0");
    
          System.out.print("\n");
        }
      }
    }
    
    • 1

    信息

    ID
    1942
    时间
    1000ms
    内存
    32MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者