URAL 1027 D++ Again
2015-08-03 11:35
381 查看
大水题,,字符串匹配。。。。。。
可是我却一直WA。。。。。呜呜呜
优先看是不是注释,,,再看在不在括号里(判断括号里东西对不对),,,,其他的东西随便了。。。。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>
#include <cstdio>
class Application
{
string file;
public:
Application()
{
char rc;
while ((rc=cin.get())!=EOF) file.push_back(rc);
}
int run()
{
bool comment=false;
int counter=0;
int last=~(1<<31);
bool answer=true;
for (int i=0;i<file.size();i++)
{
if (comment)
{
if (file[i]=='*') last=i;
else if (file[i]==')'&&last+1==i) comment=false;
}
else if (counter)
{
if (file[i]=='*'&&last+1==i)
{
counter--;
last=~(1<<31);
comment=true;
}
else if (file[i]==')')
{
counter--;
}
else if (file[i]=='(')
{
counter++;
last=i;
}
else
{
bool valid=false;
for (char* j="=+-*/0123456789)(\n";*j;j++)
if (file[i]==*j) valid=true;
if (!valid) answer=false;
}
}
else
{
if (file[i]=='(')
{
counter++;
last=i;
}
else if (file[i]==')')
{
answer=false;
}
}
}
if (comment||counter) answer=false;
cout<<(char*)(answer?"YES":"NO")<<endl;
return 0;
}
};
int main()
{
Application app;
return app.run();
}
可是我却一直WA。。。。。呜呜呜
优先看是不是注释,,,再看在不在括号里(判断括号里东西对不对),,,,其他的东西随便了。。。。
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <stack>
#include <algorithm>
#include <cstdio>
class Application
{
string file;
public:
Application()
{
char rc;
while ((rc=cin.get())!=EOF) file.push_back(rc);
}
int run()
{
bool comment=false;
int counter=0;
int last=~(1<<31);
bool answer=true;
for (int i=0;i<file.size();i++)
{
if (comment)
{
if (file[i]=='*') last=i;
else if (file[i]==')'&&last+1==i) comment=false;
}
else if (counter)
{
if (file[i]=='*'&&last+1==i)
{
counter--;
last=~(1<<31);
comment=true;
}
else if (file[i]==')')
{
counter--;
}
else if (file[i]=='(')
{
counter++;
last=i;
}
else
{
bool valid=false;
for (char* j="=+-*/0123456789)(\n";*j;j++)
if (file[i]==*j) valid=true;
if (!valid) answer=false;
}
}
else
{
if (file[i]=='(')
{
counter++;
last=i;
}
else if (file[i]==')')
{
answer=false;
}
}
}
if (comment||counter) answer=false;
cout<<(char*)(answer?"YES":"NO")<<endl;
return 0;
}
};
int main()
{
Application app;
return app.run();
}
相关文章推荐
- Fractions Again?分数拆分
- POJ - 1363 - Rails(栈)
- 用CorePlot实现类似Air Quality的柱状图滚动效果(1/2)
- HDU 2476 区间DP String painter
- POJ 1273 --Drainage Ditches【最大流模板 && dinic】
- openstack keypair实践
- DllMain和多线程死锁
- MacBook Air响一声白屏故障情况说明及解决
- Failed to start monitoring XXXXX\web.config because access was denied
- 解决nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed错误
- (FFOS Gecko & Gaia) IPC - 一种“猥琐的”IPC方式
- NSURLSession的代理为什么使用retain?
- OpenSCManager failed - 拒绝访问
- Windows环境下main()函数传入参数
- 杭电oj1789 Doing Homework again(贪心与队列完美碰撞)
- [2015hdu多校联赛补题]hdu5293 Tree chain problem
- Check GitLab API access: FAILED: Failed to connect to internal API
- public static void main(String arg[]) 详细解释
- bower failed: UNABLE_TO_VERIFY_LEAF_SIGNATURE
- 70. Climbing Stairs