首页 > 技术文章 > G - Shuffle'm Up

liuxin13 2015-07-15 17:20 原文

题目大意:
是一个洗牌游戏,首先给出两堆牌,s1,s2,先从s1上面拿一张牌再从s2上面拿一张牌依次往下可以洗好牌,然后把洗好的牌再分成两堆继续洗,直到这堆牌的顺序与给的顺序相同可以停止,当然如果洗不出给出来的顺序也可以停止
看这题首先没有什么特别好的想法,先暴力一下试试吧,,,,,,,,,,,,,,,,,,,,,
///////////////////////////////////////////////////////////////
时间竟然是0 ......真的就是一个模拟题,醉了

 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;

#define maxn 300

char L[maxn], e[maxn];

int Find(char s1[], char s2[], char s[], int C, int k)
{
    int i, j;

    for(i=j=0; i<C; i++)
    {
        L[j++] = s2[i];
        L[j++] = s1[i];
    }

    if(strcmp(L, s) == 0)
        return k;

    if(k == 1)
        strcpy(e, L);
    if(k != 1 && strcmp(e, L) == 0)
        return -1;

    strncpy(s1, L, C);
    strncpy(s2, L+C, C);

    return Find(s1, s2, s, C, k+1);
}

int main()
{
    int t=1, T;

    scanf("%d", &T);

    while(T--)
    {
        char s1[maxn]={0}, s2[maxn]={0}, s[maxn]={0};
        int C;

        scanf("%d%s%s%s", &C, s1, s2, s);

        memset(L, 0sizeof(L));

        int ans = Find(s1, s2, s, C, 1);

        printf("%d %d\n", t++, ans);

    }

    return 0;

} 

推荐阅读