[ZJ]d139: Compressed String
題目:
http://zerojudge.tw/ShowProblem?problemid=d139
----------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin >> s)
{
int ans = 0;
for(int i=0;i<s.length();i++)
{
ans = 1;
while((i+1) < s.length() && s[i] == s[i+1])
ans++, i++;
if(ans > 2)
cout << ans << s[i];
else if(ans == 2)
cout << s[i] << s[i];
else
cout << s[i];
}
puts("");
}
return 0;
}
http://zerojudge.tw/ShowProblem?problemid=d139
----------------------------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
while(cin >> s)
{
int ans = 0;
for(int i=0;i<s.length();i++)
{
ans = 1;
while((i+1) < s.length() && s[i] == s[i+1])
ans++, i++;
if(ans > 2)
cout << ans << s[i];
else if(ans == 2)
cout << s[i] << s[i];
else
cout << s[i];
}
puts("");
}
return 0;
}
留言
張貼留言