Codeforces 363C Fixing Typos 贪心+字符串模拟
2017-01-12 15:54
375 查看
点击打开链接
题意:
告诉你一个s<=2e5字符串,使得字符串满足下面条件:
1.不存在连续相同的三个字符。
2.若有连续两个字符相同 则其之后的两个字符必须不同 (不能有xxyy)
求删除最少的字符,使得字符串满足上面条件。
思路:删除最少 xxyy出现则显然删除y更好 则把字符串从头到尾加入到ans中,加入时判定即可
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+20;
string s;
int main()
{
while(cin>>s)
{
string ans;
int flag=0;
int l=s.length();
int k=0;
ans+=s[0];
//有连续两个出现时 flag=2 因为后面必须为两个不同字符 即遇到不同字符时flag-- 到flag<=0时才能添加相同字符
for(int i=1;i<l;i++)
{
if(ans[k]==s[i]&&flag==2) continue;//不能有连续3个
if(ans[k]==s[i])
{
if(flag<=0)
{
k++;
ans+=s[i];
flag=2;//有连续两个
}
}
else
{
k++;//helloo
ans+=s[i];//不相等 无连续两个
flag--;
}
}
cout<<ans<<endl;
}
return 0;
}
题意:
告诉你一个s<=2e5字符串,使得字符串满足下面条件:
1.不存在连续相同的三个字符。
2.若有连续两个字符相同 则其之后的两个字符必须不同 (不能有xxyy)
求删除最少的字符,使得字符串满足上面条件。
思路:删除最少 xxyy出现则显然删除y更好 则把字符串从头到尾加入到ans中,加入时判定即可
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+20;
string s;
int main()
{
while(cin>>s)
{
string ans;
int flag=0;
int l=s.length();
int k=0;
ans+=s[0];
//有连续两个出现时 flag=2 因为后面必须为两个不同字符 即遇到不同字符时flag-- 到flag<=0时才能添加相同字符
for(int i=1;i<l;i++)
{
if(ans[k]==s[i]&&flag==2) continue;//不能有连续3个
if(ans[k]==s[i])
{
if(flag<=0)
{
k++;
ans+=s[i];
flag=2;//有连续两个
}
}
else
{
k++;//helloo
ans+=s[i];//不相等 无连续两个
flag--;
}
}
cout<<ans<<endl;
}
return 0;
}
相关文章推荐
- CodeForces 143B - Help Kingdom of Far Far Away 2(字符串模拟)
- Codeforces 137A-Postcards and photos(模拟+贪心)
- CodeForces 25BPhone numbers(简单的字符串模拟题目)
- Codeforces 452D [模拟][贪心]
- CodeForces 637D Running with Obstacles(贪心模拟)
- CodeForces 1209 C. Anagram 字符串贪心
- 【CodeForces】508C - Anya and Ghosts(贪心 & 模拟)
- CodeForces ---596B--Wilbur and Array(贪心模拟)
- CodeForces ---596B--Wilbur and Array(贪心模拟)
- CodeForces 342B--模拟+贪心
- Codeforces 46D Parking Lot(贪心模拟)
- Codeforces 520D. Cubes 贪心模拟
- codeforces--C - Anya and Ghosts(贪心+模拟)
- Codeforces 509C. Sums of Digits 贪心 + 模拟
- 915 +-字符串【贪心+模拟】
- codeforces 96A (字符串模拟水题)
- Codeforces 508C - Anya and Ghosts (模拟 + 贪心)
- uva--1368(贪心,字符串模拟)
- Codeforces--615B--Longtail Hedgehog(贪心模拟)
- CodeForces 266C—— Below the Diagonal(模拟,贪心,递归)