1910 我想做

题目链接:http://47.96.116.66/problem.php?id=1910

做不出来就抄大佬的:https://blog.csdn.net/weixin_43272781/article/details/89436758

解题思路:

下面思考都是基于上面大佬的题解。

①ctype.h 中 有 isalnum()  判断是否是字母或数字,用这个可以减小代码长度

②' ' '这个东西打出来是错的,要么 ' \'  ',或者直接ASCII 码 39

③ 一直在考虑 当下标超出字符串长度时会不会RE,结果好像不会

我指的意思是  字符串s有效长度为10,结果拿s[11] 去和其他字符比较是否相等不会出问题。

究竟这是string的特性,还是char[] 数组也这样

解决这个问题,需要试一试这几种情况

1.char【】输入字符串后输出下标比字符串长度大的看看是什么,每次是否都一样

2.string进行相同操作

试了一下如果上一组数据长,这一组短,第一组多出来的部分在第二组中任然会保留,也就是说运气不好可能会被访问到,

但是如果当前组越界比较了,中间一定跨过了\0,那么字符串永远不可能匹配,后面相不相同已经不重要了

so,得出的结论是在做字符串匹配的时候,只要是整句输入,那么不用考虑边界问题。越界了也可以作比较。

(还是很迷啊,这个礼拜反正就做字符串的题,照此思路出问题了再回来改)

代码:

#include<iostream>
#include<cstring>
#include<string>
#include<cctype>

using namespace std;

string s,s2;
string can="i can't";
string could="i couldn't";
char sss[110];

int main()
{
    std::ios::sync_with_stdio(false);
    while (cin.getline(sss,110)){
        s = sss;
        bool flag = 1;
        while (flag){
            flag = false;
            s2 = "";
            int pos = 0;
            while (s[pos]==' '){pos++;flag = true;};

            for (int i=pos;i<s.size();i++){
                if (s[i]==' ' && !isalnum(s[i+1])){
                    flag = true;
                    continue;
                }
                s2 += s[i];
            }
            //cout << s2 <<endl;
            s = "";
            ///s2就是解决本轮空格问题后的字符串
            for (int i=0;i<s2.size();i++){
                ///处理gao
                if (i==0 && s2[i]=='g' && s2[i+1]=='a' && s2[i+2]=='o' && !isalnum(s2[i+3])){
                    flag = true;
                    i += 2;
                    continue;
                }
                if ( i && s2[i]=='g' && s2[i+1]=='a' && s2[i+2]=='o' && !isalnum(s2[i-1]) && !isalnum(s2[i+3]) ){
                    flag = true;
                    i += 2;
                    continue;
                }
                ///处理can 和 could
                if (s2[i]=='i'){
                    bool dele = true;
                    for (int j = i;j<i+7;j++){
                        if (s2[j]!=can[j-i]){dele = false;break;}
                    }
                    if (dele){
                        flag = true;
                        i+=6;///for循环会自增1
                        s += "i can";
                        while (s2[i+1]=='\'' && s2[i+2]=='t') i+=2;
                        continue;
                    }
                    dele = true;
                    for (int j = i;j<i+10;j++){
                        if (s2[j]!=could[j-i]){dele = false;break;}
                    }
                    if (dele){
                        flag = true;
                        i+=9;///for循环会自增1
                        s += "i could";
                        while (s2[i+1]=='n' && s2[i+2]=='\'' && s2[i+3]=='t') i+=3;
                        continue;
                    }
                }
                s += s2[i];
            }
            //cout<<s<<endl;
        }
        s2 = "";
        for (int i=0;i<s.size();i++){
            s2 += s[i];
            if (s[i]=='.') s2 += "duxing201606nb!";
        }
        cout << s2 << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43768644/article/details/89455194