poj 2491 Scavenger Hunt 字符串离散化
来源:互联网 发布:贵阳IBM预测大数据 编辑:程序博客网 时间:2024/05/01 20:48
题意:
乱序给出一条有n个结点的路径上的每对前前驱结点和后继结点,求原路径。
分析:
水题,直接保存每个结点的后继结点,然后找到开始结点遍历一遍输出就可以了,值得说说的是怎么用map来进行字符串离散化,即把一个字符串和一个整数对应,并给出整数能找到相应的字符串,请参考程序。
代码:
//poj 2491//sep9#include <iostream>#include <string>#include <map>using namespace std;const int maxN=512; map<string,int> m;string ans[maxN];int n;int start[maxN],next[maxN];int main(){int cases,caseNum=0;cin>>cases;while(cases--){m.clear();n=0;memset(start,0,sizeof(start));memset(next,-1,sizeof(next));int i,stepNum;cin>>stepNum;for(i=1;i<stepNum;++i){string a,b;cin>>a>>b;if(m[a]==0) m[a]=++n;if(m[b]==0) m[b]=++n;int u=m[a],v=m[b];start[v]=1;next[u]=v;}map<string,int>::iterator iter;for(iter=m.begin();iter!=m.end();++iter)ans[iter->second]=iter->first;for(i=1;i<=n;++i)if(start[i]==0)break;printf("Scenario #%d:\n",++caseNum);while(i!=-1){cout<<ans[i]<<endl;i=next[i];}cout<<endl;}return 0;}
0 0
- poj 2491 Scavenger Hunt 字符串离散化
- poj-2491-Scavenger Hunt
- POJ 2491 Scavenger Hunt 解题报告 再次利用map判重
- 2001. Scavenger Hunt
- Sicily 2001. Scavenger Hunt
- 2001. Scavenger Hunt
- 2001. Scavenger Hunt
- POJ2491 Scavenger Hunt【裸map】
- 暑假集训第三周STL L - Scavenger Hunt
- poj 1151 离散化
- POJ 3524 Bug Hunt
- poj 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- poj 2606 Rabbit hunt
- POJ 1066 Treasure Hunt
- poj 1066 Treasure Hunt
- POJ 1066 Treasure Hunt
- 【spoj8757】Kmp 概率Dp
- HttpUrlConnection和HttpClient和android-async-http框架的GET和POST请求
- 在mac上出错:No working C compiler found.
- Android 下拉刷新(刷新布局需用ScrollView包裹)
- VBScript 文件操作
- poj 2491 Scavenger Hunt 字符串离散化
- MovieClip.moveTo 方法
- bind变量的长度不同导致多个chlid cursor
- android之listView缓存机制
- sql 视图 按where条件多个字段取一个
- html5相关知识
- switch老化时间过长导致主机不通解决一例
- hdoj 超级楼梯
- MovieClip.lineTo 方法