对字符串进行 RLE 压缩,将相邻的相同字符,用计数值和字符值来代替。例如:aaabccccccddeee,则可用 3a1b6c2d3e 来代替。
数据范围:字符串长度满足
输入为a-z,A-Z的字符串,且字符串不为空,如aaabccccccddeee
压缩后的字符串,如3a1b6c2d3e
aaabccccccdd
3a1b6c2d
""" 字符串压缩 """ import sys if __name__ == "__main__": # sys.stdin = open("input.txt", "r") s = input().strip() count = 1 ans = "" for i in range(1, len(s)): if s[i] == s[i - 1]: count += 1 else: ans += str(count) + s[i - 1] count = 1 ans += str(count) + s[-1] print(ans)
比较朴素的解法
#include <iostream> #include <string> using namespace std; //相邻的相同字符-用计数值和字符值代替 //几个a几个b几个c int main(){ string str, res; int ct = 0; cin >> str; while(str.find_first_not_of(str[0]) != string::npos) { char element = str[0]; int end = str.find_first_not_of(element); ct = end; res = res + to_string(ct) + element; str = str.substr(end); } int len = str.length(); res = res + to_string(len) + str[0]; cout << res << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main() { string str; while(cin >> str) { int a = 1; for(int i = 1;i<str.size();i++) { if(str[i]==str[i-1]) { a++; } else { cout << a << str[i-1] ; a = 1; } } cout << a << str[str.size()-1] << endl; } return 0; }
/* 遍历一遍即可。 */ import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); char[] str = s.toCharArray(); int len = s.length(); int cnt = 0; for (int i = 0; i < len; i++) { cnt++; if (i == len - 1 || str[i] != str[i + 1]) { System.out.print(cnt + "" + str[i]); cnt = 0; } } } }
#include <bits/stdc++.h> using namespace std; int main() { string str; getline(cin, str); for(int i = 0; i < str.length(); i++) //遍历字符串 { int cnt = 1; //用来记录重复字符数量 while(str[i] == str[i+1]) //判断是不是字符串中的重复字符 { i++; cnt++; } cout << cnt << str[i]; //压缩后的形式,先输出重复字符的个数+1,再输出重复字符 } return 0; }
package main import ( "fmt" "strconv" "os" "bufio" ) var in=bufio.NewReader(os.Stdin) func main() { var s string fmt.Fscan(in,&s) ans:="" var pre byte cnt:=0 for i,ch:=range []byte(s){ if i==0{ pre=ch cnt=1 }else{ if ch==pre{ cnt++ }else{ ans+=strconv.Itoa(cnt)+string(pre) pre=ch cnt=1 } } } ans+=strconv.Itoa(cnt)+string(pre) fmt.Print(ans) }
#include<iostream> #include<unordered_map> using namespace std; int main() { string S; cin>>S; unordered_map<char, int> hash_map; for(auto it:S) { if(hash_map.find(it)!=hash_map.end()) { hash_map[it]++; } else { hash_map.insert(pair<char, int>(it,1)); } } string res; for(auto it:hash_map){ string tmp=to_string(it.second)+it.first; res=tmp+res; } cout<<res; } 哈希表
import sys in_str = sys.stdin.readline().strip() aa={} aa[0]=in_str[0] idx = 1 for i in range(len(in_str)-1): if in_str[i]!=in_str[i+1]: aa[idx] = in_str[i+1] idx+=1 res = '' for value in aa.values(): res+=str(in_str.count(value)) res+=value print(res)
while True: try: s = '.' s = s + input() s = s + '.' ss = '' c = 1 for i in range(1, len(s)-1): if s[i] == s[i-1] and s[i] == s[i+1]: c = c + 1 if s[i] == s[i-1] and s[i] != s[i+1]: c = c + 1 ss = ss + str(c) + s[i] c = 1 if s[i] != s[i-1] and s[i] != s[i+1]: c = 1 ss = ss + str(c) + s[i] print(ss) except: break