sgu 170 Particles

7 篇文章 0 订阅
170. Particles
time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard



A new kind of elementary particles was discovered in recent physical experiments. These particles were named "buons". It was also discovered that there exist two types of buons, which were named " Y +" (psi +) and " Y -" (psi -) buons. It became known that buons tend to form chains. Moreover, a very interesting property of buons, named "going through", was discovered: if two buons with different types are neighbors in a chain, they can easily exchange places one with other.

Because one "going through" takes very short time, the order of buons in chain can quickly change. Now the physicists are interested, how many "goings through" must happen for one chain to become other.

You are to help them. Write a program, which determines the minimal number of "goings through" which will allow one chain to become other.

Input
First two lines of input contains strings of "+" and "-" characters, describing first and second buon chains. The "+" character stands for Y + buon, and "-" character stands for Y - buon. The length of each string doesn't exceed 5000; moreover, there is at leat one buon in each chain.

Output
If it is possible to make the second chain from the first, using only "goings trough", write on the first line of output the minimal number K of "goings through" which are required. If it is impossible to make the secong chain from the first, output only integer "-1".

Sample test(s)

Input
++-+--+
-++--++

Output
4



非常诡异的一道贪心题,显然我们会得出这样一个结论,第一串的第i个‘+’号必然对应第二串的第i个‘+’;

然后我有个非常不解的地方,希望哪个看了博文的大牛给出解释或数据。


这是我AC的代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 5010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

char s[nMax],t[nMax];

void read(char *p){
     int i=0;
     char ch;
     while(1){
        ch=getchar();
        if(ch=='\n'||ch==-1)break;
        if(ch==' ')continue;
        p[i++]=ch;
     }
     p[i]='\0';
     return ;
}

int main(){
    //scanf("%s%s",s,t);
    read(s),read(t);
    if(strlen(s)!=strlen(t)){
        printf("-1\n");
        return 0;
    }
    int i=0,j=0,ans=0;
    for(;;){
        while(s[i]&&s[i]!='+') i++;
        while(t[j]&&t[j]!='+') j++;
        if(s[i]&&t[j]) { ans+=abs(i-j);i++;j++;}
        else if(s[i]||t[j]) {printf("-1\n");return 0;}
        else break;
    }
    printf("%d\n",ans);
    return 0;
}

这是我一直WA#4 的代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
#include<vector>
#include<cstdlib>

#define inf 0xfffffff
#define CLR(a,b) memset((a),(b),sizeof((a)))
#define FOR(a,b) for(int a=1;a<=(b);(a)++)

using namespace std;
int const nMax = 5010;
int const base = 10;
typedef int LL;
typedef pair<LL,LL> pij;

//    std::ios::sync_with_stdio(false);

char s[nMax],t[nMax];

void read(char *p){
     int i=0;
     char ch;
     while(1){
        ch=getchar();
        if(ch=='\n'||ch==-1)break;
        if(ch==' ')continue;
        p[i++]=ch;
     }
     p[i]='\0';
     return ;
}

int main(){
    //scanf("%s%s",s,t);
    read(s),read(t);
    if(strlen(s)!=strlen(t)){
        printf("-1\n");
        return 0;
    }
    int i=0,j,ans=0;
    for(int j=0;t[j];j++)if(s[j]!=t[j]){
        int k=i;
        while(s[k]&&s[k]!=t[j]) k++;
        if(s[k]=='\0') {
            printf("-1\n");
            return 0;
        }
        ans+=k-j;
        swap(s[j],s[k]);
        i=k;
    }
    printf("%d\n",ans);
    return 0;
}

求教大牛!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值