zoukankan      html  css  js  c++  java
  • ZOJ 3818(substr函数的使用)

    Pretty Poem

    Time Limit: 2 Seconds      Memory Limit: 65536 KB

    Poetry is a form of literature that uses aesthetic and rhythmic qualities of language. There are many famous poets in the contemporary era. It is said that a few ACM-ICPC contestants can even write poetic code. Some poems has a strict rhyme scheme like "ABABA" or "ABABCAB". For example, "niconiconi" is composed of a rhyme scheme "ABABA" with A = "ni" and B = "co".

    More technically, we call a poem pretty if it can be decomposed into one of the following rhyme scheme: "ABABA" or "ABABCAB". The symbol A, B and C are different continuous non-empty substrings of the poem. By the way, punctuation characters should be ignored when considering the rhyme scheme.

    You are given a line of poem, please determine whether it is pretty or not.

    Input

    There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

    There is a line of poem S (1 <= length(S) <= 50). S will only contains alphabet characters or punctuation characters.

    Output

    For each test case, output "Yes" if the poem is pretty, or "No" if not.

    Sample Input

    3
    niconiconi~
    pettan,pettan,tsurupettan
    wafuwafu
    

    Sample Output

    Yes
    Yes
    No
    

    题目大意:

      这道题目是说,给你一个长度不超过55的文章,要求你把其中的类似与ABABA和ABABCAB的格式串找出来,如果能够找到这样的串,就输出Yes、

    解题思路:

      这道题是自己关于substr(i,j)函数的学习,substr(i,j)指的是,从str[i]开始的长度为j的字符串。刚刚好,我们枚举substr(0,i)作为A

      substr(i,j)作为B,那么A的长度就是i,B的长度就是j,将这两个字符串进行拼接组合,看能否得到一个我们需要满足的字符串的形式。

      关于对于C的枚举,我们可以用len-(i+j)*3得到C的长度然后通过substr((i+j)*2, len-(i+j)*3 ).

    代码:

    # include<cstdio>
    # include<iostream>
    # include<cstring>
    # include<string>
    
    using namespace std;
    
    # define MAX 55
    
    char str[MAX];
    string s;
    
    int main(void)
    {
        int t; scanf("%d",&t);
        while ( t-- )
        {
            scanf("%s",str);
            int len = strlen(str);
            for ( int i = 0;i < len;i++ )
            {
                if ( islower(str[i])||isupper(str[i]) )
                    s+=str[i];
            }
            //cout<<s<<endl;
            len = s.size();
            int flag = 0;
            for ( int i = 1;i < len&&flag==0;i++ )
            {
                for ( int j = 1;j < len&&flag==0;j++ )
                {
                    string A = s.substr(0,i);
                    string B = s.substr(i,j);
                    if ( A==B )
                        continue;
                    if ( A+B+A+B+A==s )
                    {
                        flag = 1;
                        break;
                    }
                    if ( len-(i+j)*3>0 )
                    {
                        string AB = A+B;
                        string C = s.substr((i+j)*2,len-(i+j)*3);
                        if ( A==C||B==C )
                            continue;
                        if ( AB+AB+C+AB==s )
                        {
                            flag = 1;
                            break;
                        }
                    }
                }
            }
            if (flag)
                puts("Yes");
            else
                puts("No");
            s.clear();
        }
    
        return 0;
    }

      

  • 相关阅读:
    Spring BeanFactory与FactoryBean的区别及其各自的详细介绍于用法
    解决 vim 报错:the imp module is deprecated in favour of importlib
    SIFT了解,哪些方法可以在现在的AI算法中借鉴?
    CLAHE
    实际算法项目工程上手日志C/C++
    OS X 切换gcc版本
    opencv3.4.2 cmake错误:in-source builds are not allowed
    C++ opencv 计算两张图像的PSNR相似度
    如何在OS X 中使用markdown + latex混合记笔记?
    给anaconda 换源
  • 原文地址:https://www.cnblogs.com/wikioibai/p/4794950.html
Copyright © 2011-2022 走看看