双指针 UVA-492 Pig-Latin

双指针

Pig-Latin

题目大意

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WF4Dz8df-1610953464209)(C:\Users\asus\AppData\Roaming\Typora\typora-user-images\image-20210118150119034.png)]

解题思路

开两个指针, s s s 指向单词的开头, t t t 指向单词的末尾。

注意事项

1.输入的是多行文本。

参考代码

//poj GCC
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<stack>
//#define LOCAL  //提交的时候一定注释
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
typedef long long LL;
using namespace std;
const int maxn = 1e5 + 10;
const int MOD = 1e9 + 7;
const int N = 13;

int readint() {
    
    
    int x; scanf("%d", &x); return x;
}

bool is_vowel(char c) {
    
    
    c = tolower(c);
    return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

int main() {
    
    
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif
    string str;
    while (getline(cin, str)) {
    
    
        int s = 0, t = 0, len = str.size();
        while (s < len) {
    
      //还没走完
            if (!isalpha(str[s])) {
    
      //若不是字母,直接输出,t和s均后移
                printf("%c", str[s++]);
                t = s;
            } else if (isalpha(str[t])) {
    
      //若是字母,t先继续走
                t++;
            } else {
    
    
                if (is_vowel(str[s])) {
    
    
                    for(int i = s; i < t; i++) {
    
    
                        printf("%c", str[i]);
                    }
                } else {
    
    
                    for(int i = s + 1; i < t; i++) {
    
    
                        printf("%c", str[i]);
                    }
                    printf("%c", str[s]);
                }
                s = t;
                printf("ay");
            }
        }
        printf("\n");
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/Encore47/article/details/112782595