zoukankan      html  css  js  c++  java
  • 征战蓝桥 —— 2017年第八届 —— C/C++A组第7题——正则问题

    题目

    考虑一种简单的正则表达式:
    只由 x ( ) | 组成的正则表达式。
    小明想求出这个正则表达式能接受的最长字符串的长度。

    例如 ((xx|xxx)x|(x|xx))xx 能接受的最长字符串是: xxxxxx,长度是6。

    输入

    一个由x()|组成的正则表达式。输入长度不超过100,保证合法。

    输出

    这个正则表达式能接受的最长字符串的长度。

    例如,
    输入:
    ((xx|xxx)x|(x|xx))xx

    程序应该输出:
    6

    资源约定:
    峰值内存消耗(含虚拟机) < 256M
    CPU消耗 < 1000ms

    请严格按要求输出,不要画蛇添足地打印类似:“请您输入…” 的多余内容。

    注意:
    main函数需要返回0;
    只使用ANSI C/ANSI C++ 标准;
    不要调用依赖于编译环境或操作系统的特殊函数。
    所有依赖的函数必须明确地在源文件中 #include
    不能通过工程设置而省略常用头文件。

    提交程序时,注意选择所期望的语言类型和编译器类型。

    代码

    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    char s[100];
    int len,pos;
    int f()
    {
    	int m=0,tmp=0;
    	while(pos<len)
    	{
    		switch(s[pos])
    		{
    			case '(':
    				{
    					pos++;
    					tmp+=f();
    					break;
    				}
    			case ')':
    				{
    					pos++;
    					m=max(m,tmp);
    					return m;
    				}
    			case 'x':
    				{
    					pos++;
    					tmp++;
    					break;
    				}
    			case '|':
    				{
    					pos++;
    					m=max(m,tmp);
    					tmp=0;
    					break;
    				}
    		}
    	}
    	m=max(m,tmp);
    	return m;
    }
    int main()
    {
    	cin>>s;
    	len=strlen(s);
    	int ans=f();
    	cout<<ans<<endl;
    	return 0;
    }
    
  • 相关阅读:
    PAT顶级 1024 Currency Exchange Centers (35分)(最小生成树)
    Codeforces 1282B2 K for the Price of One (Hard Version)
    1023 Have Fun with Numbers (20)
    1005 Spell It Right (20)
    1092 To Buy or Not to Buy (20)
    1118 Birds in Forest (25)
    1130 Infix Expression (25)
    1085 Perfect Sequence (25)
    1109 Group Photo (25)
    1073 Scientific Notation (20)
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338604.html
Copyright © 2011-2022 走看看