• 微软编程之美初赛


    相似字符串

    描述

    对于两个长度相等的字符串,我们定义其距离为对应位置不同的字符数量,同时我们认为距离越近的字符串越相似。例如,“0123”和“0000”的距离为 3,“0123”和“0213”的距离则为 2,所以与“0000”相比,“0213”和“0123”最相似。

    现在给定两个字符串 S1 和 S2,其中 S2 的长度不大于 S1。请在 S1 中寻找一个与 S2 长度相同的子串,使得距离最小。

    输入

    输入包括多组数据。第一行是整数 T,表示有多少组测试数据。每组测试数据恰好占两行,第一行为字符串 S1,第二行为 S2。所有字符串都只包括“0”到“9”的字符。

    输出

    对于每组测试数据,单独输出一行“Case #c: d”。其中,c 表示测试数据的编号(从 1 开始),d 表示找到的子串的最小距离。

    数据范围

    1 ≤ T ≤ 100

    小数据:字符串长度不超过 1000

    大数据:字符串长度不超过 50000

    样例输入
    3
    0123456789
    321
    010203040506070809
    404
    20121221
    211
    样例输出
    Case #1: 2
    Case #2: 1
    Case #3: 1

     1 #include<stdlib.h>
     2 #include<string.h>
     3 #include<stdio.h>
     4 int main()
     5 {
     6   int T;
     7   int c=0;
     8   char s1[50000];
     9   char s2[50000];
    10   scanf("%d",&T);
    11   while(T--)
    12   {
    13       c++;
    14       scanf("%s",s1);
    15       scanf("%s",s2);
    16       int i,j;
    17       int s1l=strlen(s1);
    18       int s2l=strlen(s2);
    19       int min=s2l;
    20       for(i=0;i<=(s1l-s2l);i++)
    21       {
    22           int count =0;
    23           for(j=0;j<s2l;j++)
    24           {
    25               if(s1[i+j]!=s2[j])
    26                   count++;
    27           }
    28           if(count<min)
    29               min=count;
    30       }
    31       printf("Case #%d: ",c);
    32       if(T==0)
    33           printf("%d",min);
    34       else
    35           printf("%d\n",min);
    36   }
    37   return 0;
    38 }
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<string>
    using namespace std;
    int main()
    {
      int T;
      int c=0;
      string s1,s2;
      cin>>T;
      while(T--)
      {
          c++;
      	cin>>s1;
      	cin>>s2;
      	int i,j;
      	int s1l=s1.length();
      	int s2l=s2.length();
      	vector<int>v;
      	for(i=0;i<=(s1l-s2l);i++)
      	{
      		int count =0;
      		for(j=0;j<s2l;j++)
      		{
      			if(s1[i+j]!=s2[j])
      				count++;
      		}
      		v.push_back(count);
      	}
      	sort(v.begin(),v.end());
      	cout<<"Case #"<<c<<": ";
      	if(T==0)
      		cout<<v[0];
      	else
      		cout<<v[0]<<endl;
      }
      return 0;
    }
    


  • 相关阅读:
    Java 中的悲观锁和乐观锁的实现
    乐观锁和悲观锁的区别
    理解RESTful架构
    修复Linux下curl等无法使用 Let's Encrypt 证书
    呕心沥血 AR VR 好资源分享
    linux服务器出现大量TIME_WAIT的解决方法
    Ubuntu系统 无法删除 redis-server
    Python Flask jsonify a Decimal Error
    微信小程序 订阅消息 对接详细记录
    FTP时显示500 Illegal PORT command的解决
  • 原文地址:https://www.cnblogs.com/justcxtoworld/p/3018669.html
Copyright © 2020-2023  润新知