写的一手烂代码.jpg
写得比较匆忙,本文中可能会出现某些错字,按读音理解即可(
另一篇:https://blog.youkuaiyun.com/weixin_45775438/article/details/118707347
A - Minimum Cost Paths
防AK 😦
B - Uddered but not Herd I
记录每个字母的顺序,给定一个id
其后遍历给定的字符串每一对相邻的字母,如果后一个字母的id顺序在前一个字母之前(包括相等),则答案需要+1
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define mst(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
int id[30];
void solve()
{
string s;
string t;
cin>>s>>t;
repp(i,0,26)
id[s[i]-'a']=i;
int ans=1;
int siz=t.size();
repp(i,1,siz)
{
if(id[t[i]-'a']<=id[t[i-1]-'a'])
ans++;
}
cout<<ans<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
solve();
return 0;
}
C - Telephone
题意
n n n头牛站成一排,每头牛都有对应的种类 k k k,种类数最多 50 50 50种
给定的矩阵 s i , j s_{i,j} si,j表示第 i i i种牛是否可以向第 j j j种牛传话
现在位置为 1 1 1的牛想向位置为 n n n的牛传话,问传话的最小花费,不存在传话方案则输出 − 1 -1 −1
两头位置分别为 x , y x,y x,y的牛进行传话,花费为 ∣ x − y ∣ |x-y| ∣x−y∣
思路
考虑记录每种种类的牛的位置,存在 v e c t o r < i n t > P O S [ ] vector<int> POS[] vector<int>POS[]中
再记录每种种类可以传话的后置种类(建成一张有向图),存在 vector G[] 中
跑dijkstra最短路,每次对于当前处理的牛,遍历其种类能够传话的后置种类,再遍历这个后置种类中所有牛的位置
这样的双重遍历肯定是超时的,但大胆猜测,对于每种种类,其最多被遍历 50 50 50次,通过数组计数限制后即可
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define mst(a,b) memset(a,b,sizeof(a))
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
vector<int> G[55];
vector<int> POS[55];
int n,k;
int kind[50050];
ll dis[50050];
int vis[55];
void solve()
{
cin>>n>>k;
rep(i,1,n)
{
cin>>kind[i];
POS[kind[i]].pb(i);
}
rep(i,1,k)
{
string s; cin>>s;
repp(j,0,k)
if(s[j]=='1')
G[i].pb(j+1);
}
mst(dis,LINF);
priority_queue<P> q;
dis[1]=0;
q.push(P(0,1));
while(!q.empty())
{
P pd=q.top();
q.pop();
int u=pd.second