字符序列模式识别解题报告---模拟(很坑的题)

版权声明:转载请注明出处:https://blog.csdn.net/qq1013459920 https://blog.csdn.net/qq1013459920/article/details/84852406

                                    1311: 字符序列模式识别

时间限制: 1  内存限制: 32 MB

提交状态讨论版

题目描述

试写一个算法,识别字符序列是否为形如‘子序列1&子序列2’模式的字符序列,其中子序列2是子序列1的逆序列,0<子序列字符串长度<1000,且都为小写字母。输出YES或者NO。

输入

一行字符序列

输出

YES或NO

样例输入

hello&ollhe

样例输出

NO

题目链接:http://acm.hnucm.edu.cn/JudgeOnline/problem.php?id=1311

可以写下,多踩点坑。

一道水题,有很多坑点,导致通过率很低

坑点:

1.判断输入字符串是否全为小写字母或&

2.判断输入字符串是否只有1个&

3.判断输入字符串是否为奇数,偶数肯定不能满足字符逆序

4.子字符串长度要相等,输入字符串最中间字符一定为&

这些都判断好了之后,最后就可以字符比较是不是逆序了。

AC Code: 

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<queue>
#include<climits>
#include<set>
#include<stack>
using namespace std;
static const int MAX_N = 1e5 + 5;
inline int read(){int x = 0, f = 1;char ch = getchar();
    while(ch > '9' || ch < '0') {if(ch == '-') f = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}

int main(){
//    ios::sync_with_stdio(false);
    char str[4005];
    while(scanf("%s", str) != EOF){
        int len = strlen(str);
        int sum = 0;
        bool flag = true;
        for(int i = 0; i < len; i++){
            if(str[i] == '&'){
               sum++;
            }
            else if(str[i] <= 'z' && str[i] >= 'a'){
                flag = true;
            }
            else{
                flag = false;
                break;
            }

        }
        if(len % 2 == 0 || str[len / 2] != '&' || sum != 1){
            flag = false;
        }
        if(!flag){
            printf("NO\n");
            continue;
        }
        for(int i = 0; i < len / 2; i++){
            if(str[i] != str[len - 1 - i]){
                flag = false;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq1013459920/article/details/84852406