5.15总结

· · 个人记录

T761164 日期

思路:我们直接枚举所有可能的改动方案,即枚举MM和DD的值。
1.我们枚举MM,范围:1~12
2.我们再枚举DD,范围:1~day[MM]
3.我们再计算需要改动的值。
4.最后取最小值就可以了。
#include <bits/stdc++.h>
using namespace std;
const int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
void check()
{
    char a, b, c, e;
    cin >> a >> b >> c >> c >> e;
    int m = (a-'0')*10+(b - '0');
    int d = (c-'0')*10 + (e - '0');
    int ans = 0;
    if (!d || d > 31)
    {
        if (!m || m > 12)
        {
            ans = 2;
        }
        else 
        {
            ans = 1;
        }
    }
    else if (d == 31)
    {
        if (day[m] == 31)
        {
            ans = 0;
        }
        else if (m == 2 || m == 4 || m == 6 || m == 9 || m == 11 || !m || m >= 13 || m <= 19)
        {
            ans = 1;
        }
        else
        {
            int x = m % 10;
            if (x < 4 || x == 5 || x == 7 || x == 8 || x >= 13 && x <= 19)
            {
                ans = 1;
            }
            else 
            {
                ans = 2;
            }
        }
    }
    else if (d == 29 || d == 30)
    {
        if (m == 2 || m > 12 || m == 0)
        {
            ans = 1;
        }
        else 
        {
            ans = 0;
        }
    }
    else
    {
        if (!m || m > 12)
        {
            ans = 1;
        }
        else 
        {
            ans = 0;
        }
    }
    cout << ans << "\n";
}
int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        check();
    }
    return 0;
}

T761206 删字符

思路:
1.我们发现操作只影响字符串开头,所以不能从中间删除。
2.所以我们可以得出,最终得到的字符串一定是原来的字符串的某个后缀。
3.我们发现,每个i,都可以得到一个他的后缀, 不过前面要加上一些字符。
4.不是所有后缀都可以存在,因为有可能会提前出现。
#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
bool vis[1009];
signed main()
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        string s;
        cin >> n >> s;
        memset(vis, 0, sizeof(vis));
        int cnt = 0;
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            if (!vis[s[i] - 'a'])
            {
                cnt++;
            }
            ans = 1ll * ans + 1ll * cnt;
            vis[s[i] - 'a']=1;
        }
        cout << ans << "\n";
    }
    return 0;
}