QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#775263#5311. Master of BothExtractStarsWA 4ms14132kbC++174.9kb2024-11-23 15:14:082024-11-23 15:14:09

Judging History

你现在查看的是最新测评结果

  • [2024-11-23 15:14:09]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:14132kb
  • [2024-11-23 15:14:08]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define ft first
#define sd second

#define yes cout << "yes\n"
#define no cout << "no\n"

#define Yes cout << "Yes\n"
#define No cout << "No\n"

#define YES cout << "YES\n"
#define NO cout << "NO\n"

#define pb push_back
#define eb emplace_back

#define all(x) x.begin(), x.end()
#define all1(x) x.begin() + 1, x.end()
#define unq_all(x) x.erase(unique(all(x)), x.end())
#define unq_all1(x) x.erase(unique(all1(x)), x.end())
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3fLL

#define RED cout << "\033[91m"     // 红色
#define GREEN cout << "\033[92m"   // 绿色
#define YELLOW cout << "\033[93m"  // 蓝色
#define BLUE cout << "\033[94m"    // 品红
#define MAGENTA cout << "\033[95m" // 青色
#define CYAN cout << "\033[96m"    // 青色
#define RESET cout << "\033[0m"    // 重置

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
// typedef __int128_t i128;

typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<string, ll> psl;

typedef tuple<int, int, int> ti3;
typedef tuple<ll, ll, ll> tl3;
typedef tuple<ld, ld, ld> tld3;

typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

// std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());

template <typename T>
inline T read()
{
    T x = 0;
    int y = 1;
    char ch = getchar();
    while (ch > '9' || ch < '0')
    {
        if (ch == '-')
            y = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    return x * y;
}

template <typename T>
inline void write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    if (x >= 10)
    {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

/*#####################################BEGIN#####################################*/

int val[26][26];
int sum = 0;

struct Trie
{
    static const int MAX_LENGTH = 1e5 + 5; // 最大节点数,根据需求调整
    int son[MAX_LENGTH][26];               // 存储每个节点的子节点,26个小写字母
    int cnt[MAX_LENGTH];                   // 存储以每个节点结尾的单词数量
    int idx;                               // 当前使用的节点编号

    // 构造函数,初始化所有数据
    Trie() : idx(0)
    {
        init();
    }

    void init()
    {
        // 重新初始化所有节点
        for (int i = 0; i <= idx; ++i)
        {
            for (int j = 0; j < 26; ++j)
            {
                son[i][j] = 0;
            }
            cnt[i] = 0;
        }
        idx = 0;
    }

    // 插入一个字符串到 Trie 中
    void insert(const string &s)
    {
        int p = 0; // 从根节点开始
        for (char ch : s)
        {
            int u = ch - 'a'; // 计算字符对应的下标
            if (!son[p][u])
                son[p][u] = ++idx; // 创建新节点
            for (int j = 0; j < 26; j++)
            {
                if (j == u)
                    continue;
                val[u][j] += cnt[son[p][j]];
            }
            p = son[p][u]; // 移动到子节点
            cnt[p]++;      // 更新以当前节点结尾的单词数量
        }
        for (int i = 0; i < 26; i++)
        {
            sum += cnt[son[p][i]];
        }
    }

    // 查询字符串 s 的单词数量
    int query(const string &s)
    {
        int p = 0; // 从根节点开始
        for (char ch : s)
        {
            int u = ch - 'a'; // 计算字符对应的下标
            if (!son[p][u])
                return 0;  // 字符串不存在
            p = son[p][u]; // 移动到子节点
        }
        return cnt[p]; // 返回以当前节点结尾的单词数量
    }
} trie;
void solve()
{
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++)
    {
        string s;
        cin >> s;
        trie.insert(s);
    }
    while (q--)
    {
        string s;
        cin >> s;
        int ans = sum;
        for (int i = 0; i < 26; i++)
        {
            for (int j = i + 1; j < 26; j++)
            {
                ans += val[s[i] - 'a'][s[j] - 'a'];
                // cout << ans << " ";
            }
            // cout << "\n";
        }
        cout << ans << "\n";
        // cout << "\n";
    }
}

int main()
{
    ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
    // freopen("test.in", "r", stdin);
    // freopen("test.out", "w", stdout);
    int _ = 1;
    // std::cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}

/*######################################END######################################*/
// 链接:

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3608kb

input:

5 3
aac
oiputata
aaa
suikabudada
aba
abcdefghijklmnopqrstuvwxyz
qwertyuiopasdfghjklzxcvbnm
aquickbrownfxjmpsvethlzydg

output:

4
3
4

result:

ok 3 number(s): "4 3 4"

Test #2:

score: -100
Wrong Answer
time: 4ms
memory: 14132kb

input:

100 100
spkfvrbkfsspmnlgrdojwdqutknvzejorqxsmfgbfrhpxkrrtravhmxenjrzypkxounrbantpkaezlcnudjmwxpgqakfoxcmdjcygujdtpluovbisxmklkzzuuyziapzyrszcggjkzrwmtyolnbobubbezdwmumyzyhaogiiolictzjpxbyaamecytpnyzxlumxjkzyfavxlzdwtgrxtqcnddzfocznitlaxlpcceuelqlbmyzetlpaivxnuvuctsbjbaulmbmkangqahpdojqimvmcugjeczkgx...

output:

-1884773542
-1101743140
-1194655448
1027426855
539545060
-1379057271
-2068743575
134730462
646282290
1451436669
-1270951020
1042739898
407629222
715855671
-1651055737
-583836323
1159550350
595588384
-151415735
1264321971
-882269791
1410440901
88574122
-443163651
519582158
-1748530699
-719658618
9007...

result:

wrong answer 1st numbers differ - expected: '2368', found: '-1884773542'