藍橋杯 演算法提高 字串壓縮
題目
撰寫一個程式,輸入一個字串,然后采用如下的規則對該字串當中的每一個字符進行壓縮:
(1) 如果該字符是空格,則保留該字符;
(2) 如果該字符是第一次出現或第三次出現或第六次出現,則保留該字符;
(3) 否則,洗掉該字符,
例如,若用戶輸入“occurrence”,經過壓縮后,字符c的第二次出現被洗掉,第一和第三次出現仍保留;字符r和e的第二次出現均被洗掉,因此最后的結果為:“ocurenc”,
輸入格式:輸入只有一行,即原始字串,
輸出格式:輸出只有一行,即經過壓縮以后的字串,
#include<iostream>
#include<algorithm>
using namespace std;
int main(void)
{
//freopen("in.txt","r",stdin);
string str,strx;
//cin>>str;
getline(cin,str);
for(string::iterator i=str.begin();i<str.end();i++)
{
int k=count(str.begin(),i,*i)+1;
if(*i==' ')
strx=strx+(*i);
else if(k==1||k==3||k==6)
strx=strx+(*i);
}
cout<<strx;
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/169370.html
標籤:其他
上一篇:計算機網路之第6章 應用層
下一篇:CSP2020J組初賽游記
