sgu——340(字符串模拟)
来源:互联网 发布:mac不能用酷狗输入法么 编辑:程序博客网 时间:2024/06/05 21:20
题目地址:http://acm.sgu.ru/problem.php?contest=0&problem=340
源代码:
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <bitset>
#include <cmath>
#include <set>
#include <stack>
using namespace std;
char s[1000005];
stack<char>st; //栈,用于{}的匹配问题。
bool is_letter(char x)
{
if(x>='A'&&x<='Z')
return true;
if(x>='a'&&x<='z')
return true;
return false;
}
bool is_operator(char x)
{
if(x=='+'||x=='-'||x=='*'||x=='/')
return true;
return false;
}
bool is_digit(char x)
{
return x>='0'&&x<='9';
}
int main()
{
int i,j,k;
while(gets(s))
{
int len=strlen(s);
int now=0,cc=0;
while(!st.empty()) //清空栈
st.pop();
while(now<len&&cc<2)
{
char x=s[now++];
if(cc==0&&x!='$') //因为题中说了是以$为开始。。
continue;
if(x=='$')
{
cc++;
continue;
}
if(x==' ')
continue;
if(is_letter(x))
{
printf("<i>%c",x);
while(now<len&&(is_letter(s[now])||s[now]==' '))
{
if(s[now]!=' ')
printf("%c",s[now]);
now++;
}
printf("</i>");
continue;
}
if(is_digit(x))
{
printf("%c",x);
continue;
}
if(is_operator(x))
{
printf(" %c ",x);
continue;
}
if(x=='('||x==')')
{
printf("%c",x);
continue;
}
if(x=='}')
{
if(!st.empty())
{
if(st.top()=='^')
printf("</sup>");
else
printf("</sub>");
st.pop();
}
continue;
}
if(x=='{')
continue;
if(x=='^')
{
st.push('^');
printf("<sup>");
while(now<len&&s[now]==' ')
now++;
if(s[now]!='{')
{
if(is_letter(s[now]))
printf("<i>%c</i></sup>",s[now++]);
else if(is_operator(s[now]))
printf(" %c </sup>",s[now++]);
else if(is_digit(s[now]))
printf("%c</sup>",s[now++]);;
st.pop();
}
continue;
}
if(x=='_')
{
st.push('_');
printf("<sub>");
while(now<len&&s[now]==' ')
now++;
if(s[now]!='{')
{
if(is_letter(s[now]))
printf("<i>%c</i></sub>",s[now++]);
else if(is_operator(s[now]))
printf(" %c </sub>",s[now++]);
else if(is_digit(s[now]))
printf("%c</sub>",s[now++]);
st.pop();
}
continue;
}
}
puts("");
}
return 0;
}
- sgu——340(字符串模拟)
- sgu - 274 - Spam-filter(模拟)
- SGU - 316 - Code Tanks (简单模拟)
- sgu——344(宽搜)
- sgu——221(动态规划)
- SGU 115 Calendar (模拟)
- String——add_binary(字符串模拟加法)和multiply-strings(字符串模拟乘法)
- SGU - 519 - 3D City Model (模拟)
- 【SGU】271. Book Pile(双端队列模拟)
- SGU 101 Domino 里程碑2——开始刷sgu!
- SGU 347 字符串
- SGU 142 (字符串hash)
- sgu 326——Perspective
- sgu 330——Numbers
- sgu——169、178
- SGU 257 Debt 贪心 + 模拟
- SGU 127 Telephone directory (模拟)
- SGU 239 Minesweeper 模拟题
- Linux系统学习备忘系列3
- 【你不知道的flex】-【state状态切换之初始化方法】
- __attribute__详解
- java内部类
- js方式的页面跳转
- sgu——340(字符串模拟)
- aspectj annotation- used in spring
- VMware+Ubuntu环境安装配置SLURM
- ssh整合:spring与hibernate的整合
- Nginx 常见应用
- Hanoi Tower 汉诺塔的简单分析/C
- QT下QMainWindow类与QWidget的关系
- 从Linux程序中执行shell(程序、脚本)并获得输出结果(转)
- oracle 行列测试