湖南大学 oj LOL 字符处理函数 strstr 的应用

http://acm.hnu.cn/online/?action=problem&type=show&id=12512

LOL
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65366KB
Total submit users: 30, Accepted users: 28
Problem 12512 : No special judgement
Problem description
Your friend Andreas has a very good sense of humour. In fact, it is so good that he even enjoys to change words so that they will contain the substring lol. For example,during the 2010 FIFA World Cup in soccer he had much fun changing the word fotball to fotbalol. In order to be more ecient in his word plays, he has asked you to make a computer program that nds the minimum number of changes needed in order to transform a string into a new string containing lol as a substring. There are three legal ways to change a string: delete a character, insert a new character and replace an existing character with a new one.
Input
The first line of the input consists of a single number T, the number of test cases. Then follows T lines, containing a string S consisting of lower case letters between a and z only.
Output
For each string, output on its own line the minimum number of changes needed in order to obtain a string containing lol as a substring.
Sample Input
4
fotball
sopp
ingenting
spillolje
Sample Output
1
2
3
0
Judge Tips
0 < T <= 100 0 < |S| <= 50 (That is, the maximal string length is 50.)
要注意的是strstr函数的用法 它在库函数  string.h  里面,这样更加的简单。

char *p ,a[100],b[20];

p=strstr(a,b);

若在a中找到了b,返回的是他们中的地址;

若没有找到 返回的是NULL。

要善于利用库函数。

算法 : 题意就是 将字符串通过 插入,删除,替换使之字符串里面包含 lol这个字串。

三个一组进行查找

1有 lol的 操作步骤为0

2有lo ,ol ,ll ,l@l的操作步骤为1  @代表的是任意字符除了o。

3   1,2步不成立的话,若字符里面包含l或o的话 操作步骤为2 若不包含的话操作不走为3。


post code:

#include<stdio.h>
#include<string.h>
int main()
{
    char a[200];
    char lol[5]="lol", lo[5]="lo" ,ol[5]="ol", ll[5]="ll";
    int t;
    scanf("%d",&t);getchar();
    while(t--)
    {
        char *p=NULL;
        gets(a);
        p=strstr(a,lol); if(p!=NULL){printf("0\n");continue;}//第一步操作
        p=strstr(a,lo);  if(p!=NULL){printf("1\n");continue;}//第二步操作
        p=strstr(a,ol);  if(p!=NULL){printf("1\n");continue;}      
        p=strstr(a,ll);  if(p!=NULL){printf("1\n");continue;}                
        
        int len,i,flag=0;
        
        len=strlen(a);
        
        for( i=0; i<len-2; i++ )   //查找l@l的操作
        {
            if(a[i]=='l'&&a[i+2]=='l'){
                                     printf("1\n");
                                     flag=1;
                                     break;
                                     }   
        }  
        
        if(flag==1)continue;
        
        int sum=0;
        for( i=0; i<len; i++)  //bu
        {
         if(a[i]=='l'||a[i]=='o'){sum++;break;}     
        }
        if(sum==1)printf("2\n");
        else printf("3\n");  
              
    }
          
    
    
}







你可能感兴趣的:(湖南大学 oj LOL 字符处理函数 strstr 的应用)