Codeforces Round #299 (Div. 2)

来源:互联网 发布:java项目需求分析 编辑:程序博客网 时间:2024/05/21 15:43

A题,水题,注意单词别打错了

/*************************************************************************    > File Name: CF-299-A.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年04月15日 星期三 15时30分49秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;string str[100];int main(){    str[0] = "zero";    str[1] = "one";    str[2] = "two";    str[3] = "three";    str[4] = "four";    str[5] = "five";    str[6] = "six";    str[7] = "seven";    str[8] = "eight";    str[9] = "nine";    str[10] = "ten";    str[11] = "eleven";    str[12] = "twelve";    str[13] = "thirteen";    str[14] = "fourteen";    str[15] = "fifteen";    str[16] = "sixteen";    str[17] = "seventeen";    str[18] = "eighteen";    str[19] = "nineteen";    str[20] = "twenty";    str[30] = "thirty";    str[40] = "forty";    str[50] = "fifty";    str[60] = "sixty";    str[70] = "seventy";    str[80] = "eighty";    str[90] = "ninety";    int s;    while (~scanf("%d", &s))    {        if (s < 10)        {            cout << str[s] << endl;        }        else if (s < 20)        {            cout << str[s] << endl;        }        else if (s % 10 == 0)        {            cout << str[s] << endl;        }        else        {            int a = s - s % 10;            int b = s % 10;            cout << str[a] << "-" << str[b] << endl;        }    }    return 0;}

B,这样的数不多,dfs一遍处理出来,排个序然后就找好了

/*************************************************************************    > File Name: CF-299-B.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年04月15日 星期三 15时44分07秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;vector <LL> num;int pos;void dfs(int cur, LL sum){    num.push_back(sum);    if (cur > 9)    {        return;    }    dfs(cur + 1, sum * 10 + 4);    dfs(cur + 1, sum * 10 + 7);}int main(){    LL n;    num.clear();    dfs(1, 0);    sort(num.begin(), num.end());    while (cin >> n)    {        int pos = lower_bound(num.begin(), num.end(), n) - num.begin();        cout << pos << endl;    }}

C: 二分直接搞

/*************************************************************************    > File Name: CF-299-C.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年04月15日 星期三 16时35分46秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;int main(){    int A, B, n;    while (~scanf("%d%d%d", &A, &B, &n))    {        while (n--)        {            LL l, t, m;            cin >> l >> t >> m;            LL high = A + (l - 1) * B;            if (high > t)            {                printf("-1\n");                continue;            }            double x = (t - A) * 1.0 / B + 1;            LL left = l, right = (LL)x;            LL mid;            LL ans = -1;            while (left <= right)            {                mid = (left + right) >> 1;                LL h = A + (mid - 1) * B;                LL sum = (2 * A + (mid - 1) * B) * mid / 2;                sum -= (2 * A + (l - 2) * B) * (l - 1) / 2;                LL cnt = sum / m + ((sum % m) ? 1 : 0);                if (cnt <= t)                {                    left = mid + 1;                    ans = mid;                }                else                {                    right = mid - 1;                }            }            cout << ans << endl;        }    }    return 0;}

D,方法很多,我用Ekmp来处理的,方便判断某个前缀是否是自身的后缀,然后for一遍计数

/*************************************************************************    > File Name: CF-299-D.cpp    > Author: ALex    > Mail: zchao1995@gmail.com     > Created Time: 2015年04月15日 星期三 16时05分56秒 ************************************************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace std;const double pi = acos(-1.0);const int inf = 0x3f3f3f3f;const double eps = 1e-15;typedef long long LL;typedef pair <int, int> PLL;static const int N = 1000100;static const int mod = 1000000007;int nxt[N];char str[N];LL base2[N];int pos[N];void EXTEND_KMP(){    int lens = strlen(str);    nxt[0] = lens;    int i, j, p, L;    j = 0;    while (j + 1 < lens && str[j] == str[j + 1])    {        ++j;    }    nxt[1] = j;    int a = 1;    for (i = 2; i < lens; ++i)    {        p = nxt[a] + a - 1;        L = nxt[i - a];        if (i + L < p + 1)        {            nxt[i] = L;        }        else        {            j = max(0, p - i + 1);            while (i + j < lens && str[i + j] == str[j])            {                ++j;            }            nxt[i] = j;            a = i;        }    }}int main(){    base2[0] = 1;    for (int i = 1; i <= 1000000; ++i)    {        base2[i] = base2[i - 1] * 26 % mod;    }    int n, m;    while (~scanf("%d%d", &n, &m))    {        scanf("%s", str);        int len = strlen(str);        EXTEND_KMP();        for (int i = 1; i <= m; ++i)        {            scanf("%d", &pos[i]);            --pos[i];        }        if (m == 0)        {            cout << base2[n] << endl;            continue;        }        LL ans = 1;        bool flag = 1;        if (pos[1] > 0)        {            ans *= base2[pos[1]];        }        if (pos[1] + len > n)        {            printf("0\n");            continue;        }        int last = pos[1] + len - 1; // last position        for (int i = 2; i <= m; ++i)        {            if (pos[i] + len > n)            {                flag = 0;                break;            }            if (pos[i] > last)            {                ans *= base2[pos[i] - 1 - last];                ans %= mod;                last = pos[i] + len - 1;            }            else            {                int repos = last - pos[i] + 1;                repos = len - repos;                if (nxt[repos] != last - pos[i] + 1)                {                    flag = 0;                    break;                }                last = pos[i] + len - 1;            }        }        if (!flag)        {            printf("0\n");            continue;        }        if (last < n - 1)        {            ans *= base2[n - 1 - last];            ans %= mod;        }        cout << ans << endl;    }    return 0;}
0 0
原创粉丝点击