您的位置:首页 > 其它

[CodeM美团比赛] 优惠券

2017-06-23 23:23 127 查看

题目链接

https://www.nowcoder.com/test/5513596/summary

解题思路

http://static.nowcoder.com/b/codem/codem_qulification.zip

代码

#include<vector>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 1e5+5;
//go从前往后 rg从后往前
vector<int> go[MAXN], rg[MAXN];
int vis[MAXN],OK[MAXN];
vector<int> res; //记录数组
void dfs1(int t)
{
OK[t]=1;
for(int i=0; i<(int)rg[t].size(); ++i)
{
int v = rg[t][i]; //拿到能到t的第i个节点
if(!OK[v])
{
dfs1(v);
}
}
}

void dfs2(int s, int t)
{
if(s == t)
{
for(int i=0; i<(int)res.size(); ++i)
{
printf("%c", res[i]+'a');
}
exit(0);
}
vis[s] = 1;
for(int i=0; i<(int)go[s].size(); ++i)
{
int v = go[s][i];
if(v<0 || !OK[v])//v<0说明无路,ok记录经历过的路
continue;
if(vis[v]) //环
{
printf("Infinity!\n");
exit(0);
}
res.push_back(i);
dfs2(v,t);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=0; i<2; ++i)
for(int j=0; j<n; ++j)
{
int a;
scanf("%d",&a);
int t = a+j;
if(t>=0 && t<n)
{
go[j].push_back(t); //从i能走到t
rg[t].push_back(j); //t经过i走到的
}else
go[j].push_back(-1);
}
//初始化完成。
dfs1(n-1);
if(!OK[0])
{
return 0*printf("No solution!\n");
}
dfs2(0,n-1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: