L1-064 估值一億的AI核心代碼 (20 分)


L1-064 估值一億的AI核心代碼 (20 分)
 

AI.jpg

以上圖片來自新浪微博。

本題要求你實現一個稍微更值錢一點的 AI 英文問答程序,規則是:

  • 無論用戶說什么,首先把對方說的話在一行中原樣打印出來;
  • 消除原文中多余空格:把相鄰單詞間的多個空格換成 1 個空格,把行首尾的空格全部刪掉,把標點符號前面的空格刪掉;
  • 把原文中所有大寫英文字母變成小寫,除了 I
  • 把原文中所有獨立的 can youcould you 對應地換成 I canI could—— 這里“獨立”是指被空格或標點符號分隔開的單詞;
  • 把原文中所有獨立的 I 和 me 換成 you
  • 把原文中所有的問號 ? 換成驚嘆號 !
  • 在一行中輸出替換后的句子作為 AI 的回答。

輸入格式:

輸入首先在第一行給出不超過 10 的正整數 N,隨后 N 行,每行給出一句不超過 1000 個字符的、以回車結尾的用戶的對話,對話為非空字符串,僅包括字母、數字、空格、可見的半角標點符號。

輸出格式:

按題面要求輸出,每個 AI 的回答前要加上 AI: 和一個空格。

輸入樣例:

6
Hello ?
 Good to chat   with you
can   you speak Chinese?
Really?
Could you show me 5
What Is this prime? I,don 't know

輸出樣例:

Hello ?
AI: hello!
 Good to chat   with you
AI: good to chat with you
can   you speak Chinese?
AI: I can speak chinese!
Really?
AI: really!
Could you show me 5
AI: I could show you 5
What Is this prime? I,don 't know
AI: what Is this prime! you,don't know


***注意,如果你也是卡在第二個測試點,可以測試 [can I can] -> [can you can] 和 [can you you] -> [I can you]***
#include <bits/stdc++.h>
using namespace std;

string line, orign;


bool is_fuhao(char c) {
    return (!isspace(c) && !isalpha(c) && !isdigit(c));
}

bool is_duli(int s, int t, int limit) {

    return (((s == 0) || isspace(line[s - 1]) || is_fuhao(line[s - 1]))
                && ((t == limit) || isspace(line[t + 1]) || is_fuhao(line[t + 1])));
}

int find_duli(string s, int pos) {
    int idx;
    int len = line.length();
    while(pos < len && (idx = line.find(s, pos)) >= 0) {
        if(is_duli(idx, idx + s.length() - 1, len - 1))
            return idx;
        else
            pos += s.length();
    }
    return -1;
}

void replace_unit(string o, string t) {
    bool isUpdate = true;
    int pos = 0;
    while(pos < (int)line.length() && isUpdate) {
        isUpdate = false;
        int idx = find_duli(o, pos);
        if(idx >= 0) {
            line.replace(idx, o.length(), t);
            isUpdate = true;
            pos = idx + o.length();
        }
    }
}

void proc_replace() {
    replace_unit("I", "You");
    replace_unit("me", "You");
    replace_unit("can you", "I can");
    replace_unit("could you", "I could");
}

void del_blank() {
    bool isUpdate = true;
    while(isUpdate) {
        int len = line.length(), pos = 0;
        isUpdate = false;
        for(int i = pos; i < len; i++) {  // 結尾不會是空格,不會越界
            if(isspace(line[i]) && (isspace(line[i + 1]) || is_fuhao(line[i + 1]))) {
                line.erase(i, 1);
                pos = i;
                isUpdate = true;
                break;
            }
        }
    }
}

void to_lower_tanhao() {

    transform(line.begin(), line.end(), line.begin(), [](char c)->char {
              if(c == '?') return '!';
              else if(c == 'I') return 'I';
              return tolower(c);
    });

}

void input_and_trim() {

    getline(cin, orign);
    line = orign;

    if(!line.empty()) {
        line.erase(0,line.find_first_not_of(" "));
        line.erase(line.find_last_not_of(" ") + 1);
    }

}

int main() {

    int n;
    cin >> n;
    getchar();
    while(n --) {
        input_and_trim();
        to_lower_tanhao();
        del_blank();
        proc_replace();
        to_lower_tanhao();
        cout << orign << endl;
        cout <<"AI: "<< line << endl;
    }

}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM