Educational Codeforces Round 38 (Rated for Div. 2)A. Word Correction
2018-02-17 11:21
567 查看
题目链接:http://codeforces.com/contest/938/problem/A
题目大意:规定字母a,e,i,o,u,y为元音字母,给我们长度为n的字符串s,如果s中有连续两个元音字母,则删除后一个元音字母,直到s中无两个连续的元音字母,输出s。
思路分析:刚开始我考虑用暴力的方法,遍历字符串s ,每找到有两个连续的元音字母,就删除后一个元音字母,不过发现删除字符的代价太大了,每删除一个字符就要移动(n-i)个字符;后来发现在遍历时,无需做删除操作,只需用vis[]数组标记就行,最后输出没有被标记的字符。
ac代码:#include <bits/stdc++.h>
typedef long long LL;
const int Max = 1e5;
using namespace std;
int vis[105],n;
char str[]={'a','e','i','o','u','y'};
int main(){
string s;
cin>>n>>s;
for(int i=0; i<n; ++i){
int op1 = 0,op2 = 0;
for(int j=0; j<6; ++j){
if(s[i] == str[j])
op1 = 1;
if(s[i+1] == str[j])
op2 = 1;
}
if(op1 && op2)vis[i+1] = 1;
}
for(int i=0; i<n; ++i)
if(vis[i] == 0) printf("%c",s[i]);
printf("\n");
return 0;
}
题目大意:规定字母a,e,i,o,u,y为元音字母,给我们长度为n的字符串s,如果s中有连续两个元音字母,则删除后一个元音字母,直到s中无两个连续的元音字母,输出s。
思路分析:刚开始我考虑用暴力的方法,遍历字符串s ,每找到有两个连续的元音字母,就删除后一个元音字母,不过发现删除字符的代价太大了,每删除一个字符就要移动(n-i)个字符;后来发现在遍历时,无需做删除操作,只需用vis[]数组标记就行,最后输出没有被标记的字符。
ac代码:#include <bits/stdc++.h>
typedef long long LL;
const int Max = 1e5;
using namespace std;
int vis[105],n;
char str[]={'a','e','i','o','u','y'};
int main(){
string s;
cin>>n>>s;
for(int i=0; i<n; ++i){
int op1 = 0,op2 = 0;
for(int j=0; j<6; ++j){
if(s[i] == str[j])
op1 = 1;
if(s[i+1] == str[j])
op2 = 1;
}
if(op1 && op2)vis[i+1] = 1;
}
for(int i=0; i<n; ++i)
if(vis[i] == 0) printf("%c",s[i]);
printf("\n");
return 0;
}
相关文章推荐
- Educational Codeforces Round 38 (Rated for Div. 2) A. Word Correction
- Educational Codeforces Round 38 (Rated for Div. 2) C. Constructing Tests
- 【Educational Codeforces Round 38 (Rated for Div. 2)】 Problem A-D 题解
- Educational Codeforces Round 38 (Rated for Div. 2) D_Buy a Ticket_加源点Dijkstra
- Educational Codeforces Round 38 (Rated for Div. 2)
- Educational Codeforces Round 38 (Rated for Div. 2) B. Run For Your Prize
- Educational Codeforces Round 38 (Rated for Div. 2) 【B】 【双指针】
- Educational Codeforces Round 37 (Rated for Div. 2)【A B C】【水】【模拟】
- Codeforece Educational Codeforces Round 34 (Rated for Div. 2) (A-D)题解
- Educational Codeforces Round 37 (Rated for Div. 2)-F-SUM and REPLACE(线段树)
- Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树,区间更新)
- Educational Codeforces Round 37 (Rated for Div. 2) 【F】【线段树】
- Educational Codeforces Round 34 (Rated for Div. 2) D - Almost Difference
- Educational Codeforces Round 39 (Rated for Div. 2) E Largest Beautiful Number
- Educational Codeforces Round 36 (Rated for Div. 2)D. Almost Acyclic Graph(正解!!百度的那些拓扑排序现在都已经TLE!)
- Educational Codeforces Round 40 (Rated for Div. 2)
- Educational Codeforces Round 33 (Rated for Div. 2)
- Educational Codeforces Round 36 (Rated for Div. 2) E. Physical Education Lessons
- Educational Codeforces Round 35 (Rated for Div. 2) D. Inversion Counting
- Educational Codeforces Round 33 (Rated for Div. 2)【C】【并查集】