Personal programming language Gym
来源:互联网 发布:苏州app软件开发 编辑:程序博客网 时间:2024/06/09 20:58
题目链接:https://vjudge.net/contest/181019#problem/B
题意:定义函数,若 def a = value则a就是value;若def a = valuea def b with a = valueb则b为valueb valuea;若def a = valuea def b with a = valueb def c with b with a = valuec 则因为b中包含a,则在这个串总去掉前面出现的a,只保留最后一个出现的a,即去掉b中的a,保留a中的a,故c为valuec valueb valuea。输入一个串s,求s代表的值,输入保证s一定是def过的。
思路:将每个串与其with的串看做两个点连接起来,查询串s时,已串s为起点,在与其相连的其他串(与其相连的点)中dfs按要求输出即可,vis数组标记一个串是否输出过了。
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>#include<cstdlib>#include<sstream>#include<deque>#include<stack>#include<set>#include<map>using namespace std;typedef long long ll;typedef unsigned long long ull;const double eps = 1e-6;const int maxn = 1e5 + 20;const int maxt = 300 + 10;const int mod = 10;const int dx[] = {1, -1, 0, 0};const int dy[] = {0, 0, -1, 1};const int Dis[] = {-1, 1, -5, 5};const int inf = 0x3f3f3f3f;const int MOD = 1000;int n, m, k;map<string, int> id;map<int, string> value;bool vis[maxn];vector<int> g[maxn];vector<string> ans;void dfs(int u){ int len = g[u].size(); int v; for(int i = len - 1; i >= 0; --i){//注意一定要倒着遍历,不然def a = 1 def b = 2 def c with b with a = 3的结果会变成3 1 2;应该是3 2 1 v = g[u][i]; if(!vis[v]){ dfs(v); vis[v] = true; } } if(!vis[u]){ ans.push_back(value[u]); vis[u] = true; }}int main(){ scanf("%d", &n); getchar(); int cnt = 0; bool ok; string str, s; for(int i = 1; i <= n; ++i){ getline(cin, str);//一行一行的读 stringstream ss(str); int u; while(ss >> s){ if(s == "def"){ ss >> s; if(id.find(s) == id.end()){ id[s] = cnt++; } u = id[s]; } else if(s == "with") continue; else if(s == "="){ ss >> s; value[u] = s; } else{ if(id.find(s) == id.end()) id[s] = cnt++; g[u].push_back(id[s]);//连边 } } } cin >> s; memset(vis, false, sizeof vis); ans.clear(); dfs(id[s]);//以要查询的串对应的点为起点进行dfs,遍历结果存在ans中 int len = ans.size(); for(int i = len - 1; i >= 0; --i){//倒叙输出 cout << ans[i]; if(i) printf(" "); } cout << endl; return 0;}/*6def f1 = worlddef f2 = hellodef f3 with f2 with f1 = saydef f4 = pleasedef f5 with f3 with f4 = dontdef f6 with f3 with f2 = rather*/
阅读全文
0 0
- Personal programming language Gym
- Personal programming language Gym
- Programming Language
- The Curl Programming Language
- The Curl Programming Language
- The Programming Language Tips
- Perl Programming Language
- The Programming Language Idioms
- The Curl Programming Language
- Language Oriented Programming
- The D Programming Language
- Language Oriented Programming (翻译)
- D Programming Language
- Language Oriented Programming
- Ruby Programming language
- The C++ Programming Language
- Ruby Programming Language:
- Programming | Language | C
- mybatis-自动生成mapper接口实现类
- [扩展kmp] hdu6153 A Secret
- 遍历二叉树(递归与非递归)
- android malware
- PS入门-02-椭圆选择框基础操作
- Personal programming language Gym
- TCP的拥塞控制原理
- ubuntu16.04 opencv多版本管理与切换
- pyqt5入门—002—信号、槽
- Codeforces 845 B Luba And The Ticket
- 看涨吞没
- POIjava操作excel文件
- mybatis-mapper的复杂映射
- PostgreSQL Getting Started