zoukankan      html  css  js  c++  java
  • 【字符串入门专题1】C

    Problem Description
    Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.
     

    Input
    For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.
     

    Output
    Print the ultimate string by the book.
     

    Sample Input
    asdf sdfg asdf ghjk
     

    Sample Output
    asdfg asdfghjk

    题意:先按照前后缀长度,再按照字典序,输出两个字符串前缀和后缀最大公共长度。

    思路:因为模式串和匹配串并不是固定给出,所以要交换两个字符串的身份调用kmp两次,计算它们的最大前缀和后缀长度k1,k2,当k1和k2相同时,就比较它们的字典序,否则,按匹配过后的顺序输出。

    更正:这道题真的很有道理,如果按照我注释掉的代码提交虽然能AC,但是asdfadf 和df数据是不能过滴(代码又被小伙伴出的数据给搞掉了,让我想起了被zzq数据支配的噩梦)

    #include<stdio.h>
    #include<string.h>
    #define N 110000
    char s1[N],s2[N];
    int next[N];
    
    void get_next(char *b)
    {
    	int j,k;
    	next[0] = k = -1;
    	j = 0;
    	while(b[j]!='')
    	{
    		if(k==-1||b[j]==b[k])
    			next[++j] = ++k;
    		else
    			k = next[k];
    	}
    	return;
    }
    
    int kmp(char *a,char *b)
    {
    	int j,i;
    	get_next(b);
    	i = j = 0;
    	while(a[i]!='')
    	{
    		while(j!=-1&&a[i]!=b[j])
    			j = next[j];
    		j++;
    		i++;
    	}
        return j;
    	/*while(a[i]!=''&&b[j]!='')
    	{
    		while(j!=-1&&a[i]!=b[j])
    			j = next[j];
    		j++;
    		i++;
    	}printf("i=%d   j=%d
    ",i,j);
    	if(i == strlen(a))
    		return j;
    	return 0;*/
    }
    int main()
    {
    	int k1,k2;
    	while(scanf("%s",s1)!=EOF)
    	{
    		scanf("%s",s2);
    		k1 = kmp(s1,s2);
    		k2 = kmp(s2,s1);printf("k1=%d k2=%d
    ",k1,k2);
    		if(k1 == k2)
    		{
    			if(strcmp(s1,s2)>0)
    			{
    				printf("%s",s2);
    				printf("%s
    ",s1+k1);
    			}
    			else
    			{
    				printf("%s",s1);
    				printf("%s
    ",s2+k1);
    			}
    		}
    		else if(k1 > k2)
    		{
    			printf("%s",s1);
    			printf("%s
    ",s2+k1);
    		}
    		else
    		{
    			printf("%s",s2);
    			printf("%s
    ",s1+k2);
    		}
    	}
    	return 0;
    }
    












  • 相关阅读:
    Delphi提取EXE,DLL文件图标
    delphi 获取文件图标
    delphi 获取所有窗口标题
    SetWindowLong
    ASP.NET Core MVC添加区域Area,无法找到视图
    Unable to resolve service for type 'Microsoft.AspNetCore.Http.IHttpContextAccessor' while attempting
    pId的数据结构转children 数据结构(JS)
    VS发布网站时,报错提示:“未能将文件xxx复制到xxx,未能找到文件xx”三种解决方案!
    Multiple constructors accepting all given System.Collections.Generic.List
    javascript实现“系统可能不会保存您所做的更改 实现 是否离开”
  • 原文地址:https://www.cnblogs.com/hellocheng/p/7350059.html
Copyright © 2011-2022 走看看