Time Limit: 1000ms Memory Limit: 512MB Description iG is the most powerful team in the game League of Legends. Now you’re given a string only contains lowercase or uppercase letters, you should convert all “ig”, “IG” and “Ig” including in the string to “iG”. Input Only one line contains a string S (1 ≤ |S| ≤ 100,000) , S only contains lowercase or uppercase letters. Output Output a string represents the answer. Sample Input GGIGismostpowerfulignbIgignbnb Output GGiGismostpowerfuliGnbiGiGnbnb Hint Welcome to Jilin Collegiate Programming Contest 2019!
题目大意: 将字符串中的"ig",“IG"和"Ig"都替换成"iG”(I为大写的i)。 分析: 当我写这道题题解的时候,其实一开始我是拒绝的……这道题让我想起了蓝桥杯算法训练里的“送分啦”…… 言归正传。这道题就是一个简单的循环判断。从前到后遍历一遍,如果遇到上述三种情况,就改成"iG"。最后输出即可。 代码如下:
#include <iostream> #include <cstring> using namespace std; const int MAXSIZE = 1e5 + 5; char s[MAXSIZE]; char a[3][3] = {"ig", "IG", "Ig"}; int main() { memset(s, 0, sizeof s); cin >> s; int len = strlen(s); for(int i = 0; i < len; i++) { for(int j = 0; j < 3; j++) { if(!strncmp(s+i, a[j], 2)) //判断是否有"ig", "IG"或"Ig" { strncpy(s+i, "iG", 2); //如果存在,则改为"iG" i++; //直接跳过下一个字符 break; } } } cout << s << endl; return 0; }