hdu 2010--字符串系列

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1020

自己的代码:

#include<iostream>
#include<string>
using namespace std;
int T;
string s;
int main()
{
    cin>>T;
    while(T--)
    {
        cin>>s;
        int len = s.length();
        int sum=1;
        for(int i=0;i<len;i++)
        {
            if(s[i]==s[i+1])
                sum++;
            if(s[i]!=s[i+1])
            {
                if(sum==1)
                cout<<s[i];
                if(sum>1)
                {
                    cout<<sum<<s[i];
                    sum=1;
                }
            }
        }

       cout<<endl;
    }
    return 0;
}

看了一下别人的代码,也挺清晰的

参考下:

#include<iostream>
#include <string>
using namespace std;

void doProcess(string& str)
{
    int count,i,j;
    for (i=0;i<str.length();)
    {
        count = 1;
        for (j=i+1;j<str.length();++j)
        {
            if (str[i]==str[j])
            {//记录相同的字符个数
                count++;
            }
            else
                break;
        }
        if (count>1)
        {
            cout<<count<<str[i];
        }
        else
        {
            cout<<str[i];
        }
        i = j;//更新起始位置
    }
    cout<<endl;
}
int main()
{
    int caseNum,i;
    string strTmp;
    while (cin>>caseNum)
    {
        for (i=0;i<caseNum;++i)
        {
            cin>>strTmp;
            doProcess(strTmp);
        }
    }
    return 0;
}
时间: 2024-08-27 18:13:10

hdu 2010--字符串系列的相关文章

hdu 2010~2014

hdu 2010 求一个区间内的水仙花数. 水,但是要注意给的区间的两边大小要先排序 1 #include<stdio.h> 2 int main() 3 { 4 int n,m,i,count=0,x1,x2,x3; 5 while (scanf("%d%d",&m,&n)!=EOF) 6 { 7 if (m>n) 8 { 9 i=m; 10 m=n; 11 n=i; 12 } 13 for (i=m;i<=n;i++) 14 { 15 x1=

(原创)Python字符串系列(1)——str对象

在本博客 <Python字符串系列> 中,将介绍以下内容: Python内置的str对象及操作 字符串的格式化 Python中的Unicode字符串 Python中的正则表达式 re模块 本文将介绍Python内置的 str 类型,列举Python中字符串对象支持的方法,使用这些方法可以实现强大的字符串处理功能. 在Python 2 中,普通字符串与Unicode字符串有着明确的区分,二者都是Python内置的基本类型,例如: >>> type(str) <type '

字符串系列函数(不断跟新)

1.sprintf,sprintf_s sprintf(char* buffer, const char* format, [argument]); vs下需要加上_CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; int main() { char name[1]; int input = 9099; sprintf(name,"%d", input); system("pause&q

hdu 2721(字符串处理,位运算 暴力)

Persistent Bits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 116 Problem Description WhatNext Software creates sequence generators that they hope will produce

hdu 3973 字符串hash+线段树

http://acm.hdu.edu.cn/showproblem.php?pid=3973 Problem Description You are given some words {Wi}. Then our stupid AC will give you a very long string S. AC is stupid and always wants to know whether one substring from S exists in the given words {Wi}

hdu 4821 字符串hash+map判重 String (长春市赛区I题)

http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这几次hash军写错的变量--tmp=(j==m-1)?ah[j]:(ah[j]-ah[j-m]*base[m]);  外层循环变量是i,我写的字符串hash的几题都写成tmp=(i==0)? ah[j]:(ah[j]-ah[j-m]*base[m]); 二逼啊 题目大意: 给定一个字符串(最长10^

【HDU 2010】水仙花数

http://acm.hdu.edu.cn/showproblem.php?pid=2010 春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3.现在要求输出所有在m和n范围内的水仙花数. Solution:水仙花数又称阿姆斯特朗数,没有直接规律,只能暴力求解. 有限数列,打表算了(懒写暴力) 使用在线算法打大表: 1:  1, 2, 3, 4, 5, 6, 7, 8,

字符串系列0

基本的字符串操作 (如上图) 我会将每个函数的操作实现记录在本系列中, 因为发现网上都是单一的某些实现,每个函数可能不止一种实现 慢慢更新...... ----------------------------------------------------- Writed by Zoro /April /15th /2019 原文地址:https://www.cnblogs.com/xuzhaoping/p/10713795.html

HDU 小明系列故事——师兄帮帮忙 快速幂

小明系列故事--师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 4850    Accepted Submission(s): 1275 Problem Description 小明自从告别了ACM/ICPC之后,就开始潜心研究数学问题了,一则可以为接下来的考研做准备,再者可以借此机会帮助一些同学,尤其是漂亮的师妹.这不,班

hdu 4850 字符串构造---欧拉回路构造序列 递归+非递归实现

http://acm.hdu.edu.cn/showproblem.php?pid=4850 题意:构造长度为n的字符序列,使得>=4的子串只出现一次 其实最长只能构造出来26^4+4-1= 456979 的序列,大于该数的都是不可能的.构造方法,就是那种欧拉回路的序列,此题DFS会爆栈,手动扩展栈也可以AC...... 递归形式的开始WA了,没有细调就换非递归了,后来又想了想,虽然自己电脑上运行不了,但是先把长度按小的来,然后调试代码,然后在扩大,AC了,当时错在MOD,递归的MOD应该是26