Any foo can do it(与词法分析相关的一个asm题)
来源:互联网 发布:mac打不开exe文件 编辑:程序博客网 时间:2024/06/05 05:00
注:当开头或末尾是不带逗号的集合时,此算法不能很好的分辩,讨论情况太复杂,故此题还是得另觅他法。
题目大致如下:输入"{" 和 "," 和 "}"三个字符组成的字符串来判断是否是一个集合的表达式。
Sample Input
4
{}
{{}}
{{}},{,}}
{,,}
题目大致如下:输入"{" 和 "," 和 "}"三个字符组成的字符串来判断是否是一个集合的表达式。
Sample Input
4
{}
{{}}
{{}},{,}}
{,,}
Sample Output
Word #1: Set
Word #2: Set
Word #3: Set
Word #4: No Set
和词法分析程序有点象,如果用正则表达式,任务就轻松多了。当然那样子就没什么意思了,别人设计C++支持正则表达式也一件多么不容易的事情啊,等emacs弄熟了之后,要好好看看boost库中关于此的设计。自己写的是多么的蹩脚哦(程序尚不完善,有待调试,贴出来只为存档,现在饿了,吃饭去~):
思路:(1)先考虑由该三字符构成的最简单的集合有8个:{},{{},{}},{,},{{,}},{{,,},{},,},{{,},,}
(2)任何复杂的集合最终都是由上面这8个集合单元构成的,所以把它们都用s替换(考虑到后面的单元里面包含前面的单元,长度长的优先开始替换).
(3)替换后的就可以轻易的识别{s,.....}这样的子集合了.把经过(2)替换后得到的字符串搜索一遍,一找到s就看是否存在{s,...}形式的子集合,否则结果为no set.是则用s再替换该子集合。反复执行此过程,知道结果为no set或最后只剩下s.
//Any foo can do it
#include <iostream>
#include <string>
#include <cstddef>
using namespace std;
const string s="s";
string& replaceAll(string& context,const string& from,const string& to)
{
size_t lookhere=0;
size_t foundhere;
while((foundhere=context.find(from,lookhere))!=string::npos){
context.replace(foundhere,from.size(),to);
lookhere=foundhere+to.size();
}
return context;
}
void replace1(string& context)
{
replaceAll(context,"{{,},,}",s);
replaceAll(context,"{{,,,}}",s);
replaceAll(context,"{},{,,}",s);
replaceAll(context,"{},,,{}",s);
replaceAll(context,"{,,{,}}",s);
replaceAll(context,"{,,},{}",s);
replaceAll(context,"{{,}}",s);
replaceAll(context,"{},{}",s);
replaceAll(context,"{{,,}",s);
replaceAll(context,"{,,{}",s);
replaceAll(context,"{},,}",s);
replaceAll(context,"{,,}}",s);
replaceAll(context,"{{}",s);
replaceAll(context,"{}}",s);
replaceAll(context,"{,}",s);
replaceAll(context,"{}",s);
}
bool replaceSubset(string& context,size_t& where)
{
if(where<1) return false;
size_t headhere=where-1;
if(context.at(headhere)!='{') return false;
else{
++where;
while(context.at(where)==',' && where<context.length()-2){
++where;
if(context.at(where)=='s') ++where;
else return false;
}
if(context.at(where)!='}') return false;
else{
context.replace(headhere,where,s);
return true;
}
}
}
bool replace2(string& context)
{
while(context!=s){
size_t lookhere=0;
size_t foundhere=context.find(s,lookhere);
while((foundhere=context.find(s,lookhere))!=string::npos){
if(!replaceSubset(context,foundhere)) return false;
lookhere=foundhere+1;
}
}
if(context==s) return true;
else return false;
}
bool isSet(string str)
{
replace1(str);
return replace2(str);
}
int main()
{
int num;
cin>>num;
string str[10];
for(int i=0;i<num;i++) cin>>str[i];
for(int i=0;i<num;i++){
if(isSet(str[i])) cout<<"Word #"<<i+1<<":Set"<<endl;
else cout<<"Word #"<<i+1<<":No Set"<<endl;
}
}
#include <iostream>
#include <string>
#include <cstddef>
using namespace std;
const string s="s";
string& replaceAll(string& context,const string& from,const string& to)
{
size_t lookhere=0;
size_t foundhere;
while((foundhere=context.find(from,lookhere))!=string::npos){
context.replace(foundhere,from.size(),to);
lookhere=foundhere+to.size();
}
return context;
}
void replace1(string& context)
{
replaceAll(context,"{{,},,}",s);
replaceAll(context,"{{,,,}}",s);
replaceAll(context,"{},{,,}",s);
replaceAll(context,"{},,,{}",s);
replaceAll(context,"{,,{,}}",s);
replaceAll(context,"{,,},{}",s);
replaceAll(context,"{{,}}",s);
replaceAll(context,"{},{}",s);
replaceAll(context,"{{,,}",s);
replaceAll(context,"{,,{}",s);
replaceAll(context,"{},,}",s);
replaceAll(context,"{,,}}",s);
replaceAll(context,"{{}",s);
replaceAll(context,"{}}",s);
replaceAll(context,"{,}",s);
replaceAll(context,"{}",s);
}
bool replaceSubset(string& context,size_t& where)
{
if(where<1) return false;
size_t headhere=where-1;
if(context.at(headhere)!='{') return false;
else{
++where;
while(context.at(where)==',' && where<context.length()-2){
++where;
if(context.at(where)=='s') ++where;
else return false;
}
if(context.at(where)!='}') return false;
else{
context.replace(headhere,where,s);
return true;
}
}
}
bool replace2(string& context)
{
while(context!=s){
size_t lookhere=0;
size_t foundhere=context.find(s,lookhere);
while((foundhere=context.find(s,lookhere))!=string::npos){
if(!replaceSubset(context,foundhere)) return false;
lookhere=foundhere+1;
}
}
if(context==s) return true;
else return false;
}
bool isSet(string str)
{
replace1(str);
return replace2(str);
}
int main()
{
int num;
cin>>num;
string str[10];
for(int i=0;i<num;i++) cin>>str[i];
for(int i=0;i<num;i++){
if(isSet(str[i])) cout<<"Word #"<<i+1<<":Set"<<endl;
else cout<<"Word #"<<i+1<<":No Set"<<endl;
}
}
- Any foo can do it(与词法分析相关的一个asm题)
- PKU 2475 Any fool can do it.
- Any fool can do it!(Poj2475/Zoj2800)(递归/备忘录)
- can you do it
- I CAN DO IT
- 一个词法分析的题目
- 一个词法分析程序的实现
- 一个简单的词法分析程序
- 一个简单的词法分析程序
- 一个词法分析程序
- JAVA 与CPP的词法分析
- Python的词法分析与语法分析
- function foo(){}与foo : function(){}的区别
- 一个简单的C语言词法分析与语法分析器【原】
- HDOJ 3552 I can do it!
- HDU 3552 I can do it!
- hdu 3552 I can do it!
- HDOJ 3552 I can do it!(贪心)
- 指向类成员
- Oracle实践之导入(IMP)导出(EXP)(9)
- 多态性(一)
- 畅谈ASP.NET 查看聊天记录
- 过去写的东西,搬过来
- Any foo can do it(与词法分析相关的一个asm题)
- nBinder v 5.1.0 by Boghiu Andrei
- 线程的部分代码
- Java3D理解初级教程
- D programming language v 1.010 by Digital Mars
- CC386 v 3.31 by LADsoft
- JavaScript中的字符串操作
- 常用网址~~~~~~~~~~收集中
- NSIS v 2.25