“不简单的”删除几个字符

需要知道最少需要删除几个字符是的有连续四个字符是"tongji"。
输入描述:
多组数据 每组数据包含一个字符串 1 <= n <= 100000
输出描述:
输出一个整数表示最少需要删除的字符数,若不存在则输出"-1"。
示例1
输入
tongji
ijgnot
ttoonnggjjii
输出
0
-1
4

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<cmath>
using namespace std;

const int maxn=1e6+7;
char str[maxn];
int main ()
{
    while(scanf("%s",str))
    {
        int len=strlen(str);
        int t=-1,o=-1,n=-1,g=-1,j=-1,i=-1,Min=-1;
        for( i=0;i<len;i++)
        {
            if(str[i]=='t') t=i;
            else if(str[i]=='o'&&t!=-1) o=t;
            else if(str[i]=='n'&&o!=-1) n=o;
            else if(str[i]=='g'&&n!=-1) g=n;
			else if(str[i]=='j'&&g!=-1) j=g;
			else if(str[i]=='i'&&j!=-1)
                if(Min==-1||Min>i-j-5) Min=i-j-5;
        }
        printf("%d\n",Min);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/phthon1997/article/details/83592249