20171026测试

本文解析了两道编程竞赛题目,包括字符串转换问题(copycat)和寻找最优路径问题(running)。对于copycat问题,文章提供了详细的解题思路和代码实现;对于running问题,则介绍了如何先确定最小温度路径,然后使用Dijkstra算法找到最低热量路径的方法。

得分: 100 + 85 + 65

T1: copycat

题意: T组数据 每组数据两个字符串
字符串仅由小写字母,数字,空格,分号组成
可进行若干次操作后,能否将A串变为B串。可以输出1,否则输出0。
一次操作可将A串中所有小写字母x替换为小写字母y(x,y指任意小写字母)。

样例数据

输入

5
int x;
int y;
double a;
double aa;
float 1
float 2
string s;
double d;
print thisismycode;
float tooooooooooo;

输出

1
0
0
1
1

思路

可以讲A串转换为B串必须满足三个条件:
  • 两个字符串的长度相等
  • 两个字符串所有非字母位置对应相等
  • 字符串B中一个字母x出现的位置,在A串中这些位置的字母须相等
    那么根据三个条件思路就很明确了,建一个to数组就好,有点像映射
    不过考试的时候没考虑到:
    abcxyz
    zyxcba
    这种情况按照题意的话应该是不能转换的,标算考虑漏了…
#include<bits/stdc++.h>
using namespace std;
char s[1005],t[1005],to[30];
int lens,lent,T;
int main(){
    //freopen("copycat.in","r",stdin);
    //freopen("copycat.out","w",stdout);
    scanf("%d\n",&T);
    while(T--){
        memset(to,0,sizeof(to));
        gets(s+1);
        gets(t+1);
        lens=strlen(s+1);
        lent=strlen(t+1);
        if(lent!=lens){
            putchar('0');putchar('\n');
            continue;
        }
        int fl=1;
        for(int i=1;i<=lens;++i){
            if(s[i]!=t[i]){
                if(s[i]<'a'||s[i]>'z'||t[i]<'a'||t[i]>'z'){ fl=0; break; }
                if(!to[s[i]-'a']) to[s[i]-'a']=t[i];
                else if(to[s[i]-'a']!=t[i]){ fl=0; break; }
            }
        }
        if(!fl) putchar('0');
        else putchar('1');
        putchar('\n');
    }
    return 0;
}

T2:running

题意: 给你一个无向图,每条边上有一个温度和通过时间,求从起点到终点经过的所有道路中最高温度最低的前提下,到达终点的热量最低。

第一行两个整数n,m
接下来m行四个整数a,b,t,c代表双向道路的两个端点,温度和通过所需时间

样例数据

输入

5 6
1 2 1 2
2 3 2 2
3 4 3 4
4 5 3 5
1 3 4 1
3 5 3 6
1 5

输出

3 24

按照题意,首先使通过的所有路径中最大温度最小
那么先把边按温度从小到大排序,一直加边直到起点和终点联通,即可求出最小温度
再把所有温度满足条件的边连起来,跑一次最短路即可,要注意优化常数
dijkstra算法也可以加ex数组判断,会更快一些
#include<bits/stdc++.h>
using namespace std;
int n,m,t,s,tot,lim;
int first[500005],fa[500005];
long long dis[500005];
bool vis[500005];
struct node{
    int next,to;
    long long v;
}e[2000005];
struct node1{
    int x,y,z,c;
}a[1000005];
priority_queue< pair<int,int> >q;
void inser(int x,int y,long long z){
    tot++;
    e[tot].next=first[x];
    first[x]=tot;
    e[tot].to=y;
    e[tot].v=z;
    tot++;
    e[tot].next=first[y];
    first[y]=tot;
    e[tot].to=x;
    e[tot].v=z;
    return;
}
int read(){
    int i=0,f=1;char ch=getchar();
    for(;ch>'9'||ch<'0';ch=getchar()) if(ch=='-') f=-1;
    for(;ch<='9'&&ch>='0';ch=getchar()) i=(i<<3)+(i<<1)+ch-'0';
    return i*f;
}
int buf[1024];
inline void w(long long x){
    if(!x){putchar('0');return ;}
    while(x){buf[++buf[0]]=x%10;x/=10;}
    while(buf[0]) putchar(buf[buf[0]--]+'0');
    return;
}
inline bool cmp(const node1&a,const node1&b){
    return a.z<b.z;
}
inline int find(int x){
    if(fa[x]!=x) return fa[x]=find(fa[x]);
    else return x;
}
void dij(){
    q.push(make_pair(0,t));
    for(int i=1;i<=n;++i) dis[i]=-1;
    dis[t]=0;vis[t]=true;
    while(!q.empty()){
        int x=q.top().second;
        q.pop();vis[x]=false;
        for(int u=first[x];u;u=e[u].next){
            int to=e[u].to;
            if(dis[x]+e[u].v<dis[to]||dis[to]==-1){
                dis[to]=dis[x]+e[u].v;
                if(!vis[to]){
                    vis[to]=true;
                    q.push(make_pair(-dis[to],to));
                }
            }
        }
    }
    return;
}
int main(){
    n=read();m=read();
    for(register int i=1;i<=n;++i) fa[i]=i;
    int x,y,z,c;
    for(register int i=1;i<=m;++i){
        a[i].x=read();a[i].y=read();a[i].z=read();a[i].c=read();
    }
    t=read();s=read();
    sort(a+1,a+m+1,cmp);
    int fl=0;
    for(register int i=1;i<=m;++i){
        if(fl&&a[lim].z<a[i].z) break;
        int px=find(a[i].x),py=find(a[i].y);
        fa[px]=py;
        lim=i;
        if(!fl){
            px=find(t);py=find(s);  
            if(px==py) fl=1;
        }
    }
    for(register int i=1;i<=lim;++i) inser(a[i].x,a[i].y,(long long)a[i].z*a[i].c);
    dij();
    w(a[lim].z);putchar(' ');
    w(dis[s]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值