zoukankan      html  css  js  c++  java
  • HDU-1020 Encoding (字符串)

    突然发现刷水题魔摸鱼好开心...

    题意:

    给出一个字符串s,然后对子串进行如下统计

    例如:输出连续子串的字母以及个数

    思路:

     用两个数组来统计,一个统计个数,一个统计字符

    Sample Input
    2
    ABC
    ABBCCC
    Sample Output
    ABC
    A2B3C
    

    完整代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #define rep(i,n,m) for(i=n;i<m;i++)
    const int maxn= 1e5+10;
    int ans[maxn];
    char ansc[maxn];
    using namespace std;
    int main(){
        int n,cnt,i ;
        cin>>n;
        while(n--){
            cnt = 0;
            string s;
            cin>>s;
            char c;
            memset(ans,0,sizeof(ans)); 
            rep(i,0,s.size()){
                if(i==0) {
                    c = s[i];
                    ans[cnt]++;
                    ansc[cnt] = c;
                }
                else
                {
                    if(c==s[i]) {
                        ans[cnt]++;
                    }else{
                        c = s[i];
                        cnt++;
                        ans[cnt]++;
                        ansc[cnt] = c;
                    }
                }
            }
            rep(i,0,cnt+1){
                if(ans[i]==0) continue;
                else if(ans[i]==1) cout<<ansc[i];
                else cout<<ans[i]<<ansc[i];
            }
            cout<<endl; 
        }    
    }
  • 相关阅读:
    WordCount结对项目
    第一周作业:一些感想
    第一次作业
    Spring Cloud 微服务实战笔记
    解决jest处理es模块
    领域驱动设计(DDD:Domain-Driven Design)
    测试
    whistle
    日记(2018-11-07)
    ubuntu中使用机密数据Secrets
  • 原文地址:https://www.cnblogs.com/Tianwell/p/11195392.html
Copyright © 2011-2022 走看看