HDU 4915 Parenthese sequence
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4915
解题报告:从前往后遍历一次,每次判断')'的数目是不是满足 n < (i +1)/ 2,从后往前遍历一次,每次判断'('的数目是不是满足 n <= (len - i) / 2;
这样就可以判断出这个序列是否存在匹配的序列。接下来就是判断是Many还是Unique的情况,因为数据是10的六次方,所以遇到问号改成'(' 或')'暴力判断是不行的,但是我们可以判断出只要(和)的数目小于等于len / 2而且有三个连续的?那句是Many,否则再进行暴力判断,这样就可以大大减小时间复杂度。

1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int maxn = 1000000+5; 7 char str[maxn]; 8 9 int judge(char* str) 10 { 11 int len = strlen(str); 12 int a = 0,b = 0; 13 for(int i = 0,j = len- 1;i < len,j >= 0;++i,--j) 14 { 15 if(str[i] == ')') a++; 16 if(str[j] == '(') b++; 17 if(a > (i + 1) / 2) return 0; 18 if(b > (len - j) / 2) return 0; 19 } 20 return 1; 21 } 22 int main() 23 { 24 // freopen("1005.in","r",stdin); 25 int T = 0; 26 while(scanf("%s",str)!=EOF) 27 { 28 T++; 29 int ans; 30 ans = judge(str); 31 int l = strlen(str); 32 if(l & 1) ans = 0; 33 if(!ans) 34 { 35 puts("None"); 36 continue; 37 } 38 int len = strlen(str); 39 int a = 0,b = 0,c = 0,f = 0,M = 0; 40 for(int i = 0;i < len;++i) 41 { 42 if(str[i] == '(') a++; 43 if(str[i] == ')') b++; 44 if(str[i] == '?') 45 { 46 f++; 47 c++; 48 } 49 else 50 { 51 M = max(M,f); 52 f = 0; 53 } 54 } 55 if(a >= len / 2 || b >= len / 2 || c <= 1) 56 { 57 puts("Unique"); 58 continue; 59 } 60 if(M >= 3) 61 { 62 puts("Many"); 63 continue; 64 } 65 ans = 0; 66 for(int i = 0;i < len;++i) 67 if(str[i] == '?') 68 { 69 int tt = 0; 70 str[i] = '('; 71 tt += judge(str); 72 str[i] = ')'; 73 tt += judge(str); 74 if(tt == 2) 75 { 76 ans = 1; 77 break; 78 } 79 str[i] = '?'; 80 } 81 printf(ans? "Many\n":"Unique\n"); 82 } 83 return 0; 84 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· Vite CVE-2025-30208 安全漏洞
· 《HelloGitHub》第 108 期
· MQ 如何保证数据一致性?
· 一个基于 .NET 开源免费的异地组网和内网穿透工具