zoukankan      html  css  js  c++  java
  • hihoCoder#1082 然而沼跃鱼早就看穿了一切

    原题地址

    字符串匹配+替换

    注意替换串和原串长度是不等的,所以替换完还要进行收缩

    可以顺带练习一下KMP

    代码:

     1 #include <iostream>
     2 #include <cstring>
     3 
     4 using namespace std;
     5 
     6 void kmp(char *line, int len) {
     7   char p[] = "marshtomp";
     8   char t[] = "fjxmlhx";
     9   int next[10] = {-1, 0, 0, 0, 0, 0, 0, 0, 1, 0};
    10   int i = 0;
    11   int j = 0;
    12 
    13   while (i < len && j < 9) {
    14     if (j < 0 || tolower(line[i]) == p[j]) {
    15       i++;
    16       j++;
    17     }
    18     else
    19       j = next[j];
    20     if (j == 9) {
    21       memcpy(line + i - 9, t, 7);
    22       line[i - 9 + 7] = 0;
    23       line[i - 9 + 8] = 0;
    24       j = 0;
    25     }
    26   }
    27 }
    28 
    29 void shrimp(char *line, int len) {
    30   int i = 0;
    31   int j = 0;
    32 
    33   while (j < len) {
    34     if (line[j] == 0)
    35       j++;
    36     else
    37       line[i++] = line[j++];
    38   }
    39   line[i] = 0;
    40 }
    41 
    42 int main() {
    43   char line[256];
    44 
    45   while (gets(line)) {
    46     int len = strlen(line);
    47     kmp(line, len);
    48     shrimp(line, len);
    49     cout << line << endl;
    50   }
    51 
    52   return 0;
    53 }
  • 相关阅读:
    php函数总结
    文本框输入限制
    e.keyCode和e.which使用
    transform总结
    wampSever的mysql操作
    linux命令总结
    nginx总结
    微雪的stm32学习资料
    串口+RS485驱动
    cubemx+stm32串口学习汇总资料
  • 原文地址:https://www.cnblogs.com/boring09/p/4355969.html
Copyright © 2011-2022 走看看