poj 1580 String Matching 【字符串处理】

/*题意:移动字符串一与另外的一个字符串匹配,找最多的匹配个数
策略  暴力  就是将一个字符串固定,然后用另一个字符串从左往右来跟这一字符串来比较*/
#include <stdio.h>
#include <string.h>
#define M 10000
char a[M], b[M];
int gcd(int a, int b)
{
	if(b == 0) return a;
	else return gcd(b, a%b);
}
int main()
{
	while(scanf("%s", a) != EOF){
		if(strcmp("-1", a) == 0) break;
		scanf("%s", b);
		int la = strlen(a);
		int lb = strlen(b);
		int max = 0;
		for(int i = 0; i < la; i ++){  //将第二个字符串固定,然后移动第一个字符串
			int temp = 0;
			for(int j = 0, k = i; j < lb&&k< la; j++, k++){
				if(a[k] == b[j]){
					++temp;
				}
			}
			if(max < temp) max = temp;
		}
		for(int i = 0; i < lb; i ++){  <span style="font-family: Arial, Helvetica, sans-serif;">//将第一个字符串固定,然后移动第二个字符串</span>
			int temp = 0;
			for(int j = 0, k = i; j < la&&k<lb; j++, k++){
				if(a[j] == b[k]){
					++temp;
				}
			}
			if(max < temp) max = temp;
		}
		//printf("%d\n", max);
		max*=2;
		printf("appx(%s,%s) = ", a, b);
		if(max == 0) {
			printf("0\n");
			continue;
		}
		if(max == la+lb){
			printf("1\n");
			continue;
		}
		int temp = gcd(la+lb, max);
		//printf("%d\n", temp);
		printf("%d/%d\n", max/temp, (la+lb)/temp);
	}
}

题目链接http://poj.org/problem?id=1580

时间: 2024-09-27 06:55:20

poj 1580 String Matching 【字符串处理】的相关文章

poj 1580 String Matching(比较字符串的相似程度,四个for循环即可)

String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3717   Accepted: 1913 Description It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical? And how close

[POJ] String Matching

String Matching Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4074   Accepted: 2077 Description It's easy to tell if two words are identical - just check the letters. But how do you tell if two words are almost identical? And how close

NYOJ 5 Binary String Matching【string find的运用】

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

NYOJ 5 Binary String Matching (kmp 字符串匹配)

Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alphabet consist only '0' and '1'. Your task is only to tell how many times does A appear as a substring of B? For example, the text string B is '100111011

POJ 1035 Spell Check 字符串处理

被这样的题目忽悠了,一开始以为使用Trie会大大加速程序的,没想到,一不小心居然使用Trie会超时. 最后反复试验,加点优化,终于使用Trie是可以过的,不过时间大概难高于1500ms,一不小心就会超时. 看来这是一道专门卡Trie的题目,只好放弃不使用Trie了. 也得出点经验,如果字符串很多,如本题有1万个字符串的,那么还是不要使用Trie吧,否则遍历一次这样的Trie是十分耗时的,2s以上. 于是使用暴力搜索法了,这里的技巧是可以使用优化技术: 1 剪枝:这里直接分组搜索,分组是按照字符串

poj 1200 --- 不错的字符串HASH构造方法

题目:http://poj.org/problem?id=1200 题意:给一个字符串,给定n和nc,字符串里最多有nc个不同的字符,问长度为n的不同子串最多有几个 和上一篇现场赛那个一样,也是难在判重处理不好会超时 方法:将长度为n的子串映射为一个nc进制的数,开一个大数组,判断是否重复 #include <cstdio> #include <cstring> #include <algorithm> #include <string> #include

KMP String Matching Algorithm

This is a program based on Knuth-Morris-Pratt String Matching Algorithm (a.k.a KMP algorithm), which can solve the problem POJ 3461. 1 import java.io.*; 2 import java.util.*; 3 4 class Input { 5 private Scanner in; 6 private StringTokenizer tok; 7 8

LeetCode | 1408. String Matching in an Array数组中的字符串匹配【Python】

LeetCode 1408. String Matching in an Array数组中的字符串匹配[Easy][Python][字符串] Problem LeetCode Given an array of string words. Return all strings in words which is substring of another word in any order. String words[i] is substring of words[j], if can be o

noi Big String 超级字符串

//来自2017青岛信息竞赛第一题 9269:Big String超级字符串 查看 提交 统计 提问 总时间限制:  10000ms 单个测试点时间限制:  1000ms 内存限制:  131072kB 描述 fish在无数次oi竞赛的狱炼之后,悟出一个真理,往往越容易的题目,陷阱越深.由此,fish创作了这道题目. fish首先给出两个短串A='4567' (4个字符), B='123'(3个字符).反复进行如下的操作得到一个长串C. (1)C=B+A (例如:A='4567' ,B='123