hackerrank.com在Obj-C上找到-hackerrank解决方案

时间:2014-12-23 14:10:28

标签: objective-c

我试图在Obj-C上解决https://www.hackerrank.com/challenges/find-hackerrank,并通过xCode获得正常输出,但不是通过hackerrank"运行代码"按钮。

xCode输出:

http://monosnap.com/image/SBZTETlf6WjEiNPr50zpYQ7G119wC7

hackerrank输出:

http://monosnap.com/image/9WApf0Q5ptYAkN7Oc1ipw9tBBauSJA

所以看到不同的输出是奇怪的。

我的代码:

#import <Foundation/Foundation.h>

int main()
{
    NSFileHandle *input;
    NSData *inputData;

    NSString *match = @"hackerrank";
    int amount;
    NSString *str;

    input = [NSFileHandle fileHandleWithStandardInput];
    inputData = [input availableData];
    amount = [[[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding] intValue];

    for (int j = 0; j < amount; j++)
    {
        inputData = [input availableData];

        str = [[NSString alloc] initWithData:inputData encoding:NSUTF8StringEncoding];
        str = [str stringByReplacingOccurrencesOfString:@"\n" withString:@""];

        NSArray *redexArr = @[match,
                              [NSString stringWithFormat:@"^%@.+", match],
                              [NSString stringWithFormat:@".+%@$", match]
                              ];

        for (int i = 2; i>=-1; i--)
        {
            if (i <= -1)
            {
                printf("-1\n");
            } else
            {
                NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", redexArr[i]];
                if ([pred evaluateWithObject:str])
                {
                    printf("%d\n", i);
                    break;
                }
            }
        }
    }
    return 0;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

Xcode和HackerRank中的运行之间的区别在于,当您在本地运行时,调用

[input availableData];
当程序从控制台读取下一行时,

停止。这样,您可以多次拨打availableData,每次都可以拨打下一行。

在HackerRank上,虽然第一次调用availableData会获取整个文件,但不会让你的程序剩余部分消耗掉。

您可以通过逐行读取文件或阅读整个内容并将其拆分到行尾标记来解决此问题。

以下是您通过HackerRank的所有测试的固定提交:

#import <Foundation/Foundation.h>

int main()
{
    NSFileHandle *input;

    NSString *match = @"hackerrank";
    int amount;

    input = [NSFileHandle fileHandleWithStandardInput];
    NSArray *inputData = [[[NSString alloc] initWithData:[input availableData] encoding:NSUTF8StringEncoding] componentsSeparatedByString: @"\n"];
    amount = [inputData[0] intValue];

    for (int j = 1; j <= amount; j++)
    {
        NSString *str = inputData[j];

        NSArray *redexArr = @[match,
                              [NSString stringWithFormat:@"^%@.+", match],
                              [NSString stringWithFormat:@".+%@$", match]
                              ];

        for (int i = 2; i>=-1; i--)
        {
            if (i <= -1)
            {
                printf("-1\n");
            } else
            {
                NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", redexArr[i]];
                if ([pred evaluateWithObject:str])
                {
                    printf("%d\n", i);
                    break;
                }
            }
        }
    }
    return 0;
}