牛客 19-5-3 QAQ
2024-09-05 11:55:16
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
int main()
{
char a[];
scanf("%s",a);
LL s1=,sum=,len=strlen(a);
if(a[]=='Q') s1++;
for(int i=;i<len;i++)
{
if(a[i]=='Q'){s1++;}
if(a[i]=='A'&&s1!=)
{
LL s2=,s=s1;
if(a[i-]=='Q') s--;// 小心越界
for(int j=i+;j<strlen(a);j++)
if(a[j]=='Q') s2++;
sum+=s*s2;
}
}
cout<<sum<<endl;
}
// 超时代码
#include<iostream>
#include<string>
using namespace std;
int count=;
void bfs(string s,int i,int flag)
{
if(i>=(int)s.length()) return ;
if(flag==)
{
if(s[i]=='Q')
{
bfs(s,i+,flag+);
}
else bfs(s,i+,flag);
}
if(flag==)
{
if(s[i]=='A')
{
bfs(s,i+,flag+);
}
bfs(s,i+,flag);
}
if(flag==)
{
if(s[i]=='Q')
{
count++;
}
bfs(s,i+,flag);
}
}
int main()
{
string s;
cin>>s;
for(int i=;i<s.length()-;i++)
bfs(s,i,);
cout<<count<<endl;
}
相比两个方法 第一个少了回溯次;
B题:
#include <iostream>
using namespace std;
#define N 'W'+ 'W' + '#'
#define FOR(i,l,r) for(int i = l;i < r;i++)
char a[][];
int judge()
{
if(a[][] + a[][] + a[][] == N || a[][] + a[][] + a[][] == N)return ;
FOR(i,,)
if(a[i][]+a[i][]+a[i][]==N || a[][i]+a[][i]+a[][i] == N)return ;
return ;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int flag = ;
FOR(i,,)scanf("%s",a[i]);
if(!judge())
{
FOR(i,,)FOR(j,,)
if(a[i][j] == 'W')
{
a[i][j]='#';
if(judge())flag = ;
a[i][j]='W';
}
flag?puts("Emmm"):puts("Alice");
}
else puts("Bob");
}
return ;
}
代码精炼。正常要写100+;
最新文章
- mybatis进阶
- WebSocket桌面客户端工具
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
- poj 1006 中国剩余定理解同余方程
- Linux运维
- [Hadoop源码解读](四)MapReduce篇之Counter相关类
- photoshop中rgb与索引模式的区别
- nginx access log logrotate配置
- linux主机load average的概念&;&;计算过程&;&;注意事项
- bppm与AD域集成
- ios app网址说明
- sehll 小脚本的应用
- app后端设计(6)-- LBS
- [C]static变量详解
- system函数的应用
- arcgis api for flex 开发入门
- Delphi在Listview中加入Edit控件
- 微信小程序——template的使用方法
- [ios]Xcode常用快捷键
- windows 设置CapsLock键开启大写后使用shift键取消大写
热门文章
- 第一章 T-SQL查询和编程基础 T-SQL语言基础(1)
- kinit: Bad encryption type while getting initial credentials
- 搭建私有CA并基于OpenSSL实现双向身份认证
- windows下双击可运行的Java软件打包方案(转)
- 并查集+启发式合并+LCA思想 || 冷战 || BZOJ 4668
- 用xshell连接l自己的inux
- 高性能mysql 第7章 mysql高级特性之分区表
- 分布式中 CAP BASE ACID 理解(转载)
- mysql 主从 Last_IO_Errno错误代码说明
- k8sConfigMap资源