Codeforces 318 A. Even odds B. sereja and array

Source: Internet
Author: User

A. Even odds

Here we will give you N and K to arrange an even number in the odd row from 1 to n in a new sequence and output the number at the K position.

For example, after 10 3 beats, the number of 1 3 5 7 9 2 4 6 8 10 3rd is 5.

//cf 318 A//2013-06-18-20.30#include <iostream>using namespace std;int main(){    __int64 n, k;    while (cin >> n >> k)    {        if (k <= (n+1)/2)            cout << (k-1)*2 + 1 << endl;        else        {            k -= (n+1)/2;            cout << k*2 << endl;        }    }    return 0;}

B. sereja and array

Find the number of strings starting with "heavy" and ending with "Metal.

My idea is to mark the locations of "heavy" and "Metal" and then calculate the number of parts ending with each "Metal" and then add them together.

//cf 318 B//2013-06-18-21.01#include <stdio.h>#include <string.h>#include <iostream>using namespace std;const int maxn = 1000006;char str[maxn];int s[maxn];int e[maxn];int main(){    while (scanf("%s", str) != EOF)    {        memset(s, 0, sizeof(s));        memset(e, 0, sizeof(e));        int l = strlen(str);        for (int i = 0; i < l-4; i++)        {            if (str[i] == 'h' && str[i+1] == 'e' && str[i+2] == 'a' && str[i+3] == 'v' && str[i+4] == 'y')            {                s[i] = 1;                i += 4;                continue;            }            if (str[i] == 'm' && str[i+1] == 'e' && str[i+2] == 't' && str[i+3] == 'a' && str[i+4] == 'l')            {                e[i] = 1;                i += 4;                continue;            }        }        __int64 ans = 0;        for (int i = 1; i < l; i++)        {            if (e[i])                ans += s[i-1];            s[i] += s[i-1];        }        cout << ans << endl;    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.