L1-8 估值一亿的AI核心代码 (20 分) pta

题目连接:https://pintia.cn/problem-sets/1112331490154930176/problems/1112332827798167559

L1-8 估值一亿的AI核心代码 (20 分)

AI.jpg

以上图片来自新浪微博。

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

  • 无论用户说什么,首先把对方说的话在一行中原样打印出来;
  • 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 把原文中所有大写英文字母变成小写,除了 I
  • 把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 把原文中所有独立的 I 和 me 换成 you
  • 把原文中所有的问号 ? 换成惊叹号 !
  • 在一行中输出替换后的句子作为 AI 的回答。

输入格式:

输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。

输出格式:

按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。

输入样例: 略

输出样例: 略 

终究还是来补了这个傻逼题,虽然心里极度不愿意,但是还是写了不到一个小时

  整个一个傻逼题,首先先把前后空格去掉,然后我们可以考虑用“#”先代替非数字和字母,然后前后加个“#”,(为了方便处理找出来需要修改的子串的情况,因为子串可能在中间,首尾,这样可以统一处理一下),然后找到那些符合的位置,标记掉,

接着判断ans +=   就 OK了,代码如下,逻辑想好了就很好写了:

 

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
inline int cmp(db a,db b){ return sign(a - b);}
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
int fa[maxn];
int Find(int x) {   if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
ll c,n,k;
string s;
vector<int>v[6];
bool viss[maxn];
string mode;
void check(string str,int op){
    int len1 = str.size(),len2 = mode.size();
    for(int i = 0;i < len2;i++){
        int k = i,j;
        for(j = 0;j < len1 && i < len2;j++,i++){
            if(mode[i] != str[j]) break;
        }
        if(j == len1) v[op].push_back(i - len1  );
        i = k;
    }
}
void FFind()
{
     check("#can#you#",1);
     check("#could#you#",2);
     check("#me#",3);
     check("#I#",4);
     return ;
}
string ans;
char str[maxn];int  vis[maxn];
void getans(int flag,int &i,string str)
{

  while(vis[i + 1] == flag) i++;
    ans += str;
     return ;
}
int main() {
    int t;
    cin >> t;
    getchar();
    while(t--){
        cin.getline(str,2000,'\n');
        int len = strlen(str);
        s = ans = "";
        cout << str << endl;
        memset(vis,0,sizeof(vis));
        for(int i = 0;i < 6;i++) v[i].clear();
        int flag = 0;
        for(int i = 0; i < len;i++){
            if(str[i] == ' '){
                while(str[i + 1] == ' ') i++;
                if(str[i + 1] <= 'z' && str[i + 1] >= 'a' || (str[i + 1] >= 'A' && str[i + 1] <= 'Z') ||
                  (str[i + 1] >= '0' && str[i + 1] <= '9') ){
                    if(flag)
                    s += ' ';
                }
                continue;
            }
            s += str[i];
            flag = 1;
        }
        int id = s.size() + 1;
        for(int i = s.size() - 1;i >= 0;i--)
            if(str[i] != ' '){
                break;id = i;
            }
        s = s.substr(0,id - 1);
        for(int i = 0; i < s.size();i++)  if(s[i] <= 'Z' && s[i] >= 'A' && s[i] != 'I') s[i] += 32;
        mode = "#";
        for(int i = 0;i < s.size();i++)  if((s[i] < 'a' || s[i] > 'z') && s[i] != 'I') mode += "#";  else mode += s[i];
        mode += "#" ;
        FFind();
        for(auto d:v[1])    for(int j = d;j < d + 7;j++) vis[j] = 1;
        for(auto d:v[2])    for(int j = d;j < d + 9;j++) vis[j] = 2;
        for(auto d:v[3])    for(int j = d;j < d + 2;j++) vis[j] = 3;
        for(auto d:v[4])      vis[d] = 4 ;
         ans = ""; cout << "AI: ";
        for(int i = 0;i < s.size();i++){
            if(!vis[i]){
                if(s[i] == '?') s[i] = '!';
                ans += s[i];
            }else if(vis[i] == 1)   getans(1,i,"I can");
             else if(vis[i] == 2)   getans(2,i,"I could");
             else if(vis[i] == 3)   getans(3,i,"you");
             else  getans(4,i,"you");
        }
        cout << ans << endl;
    }
    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值