POJ 1580-String Matching(枚举)

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

题意:给出两个串a,b,求它们的最大匹配度。最大匹配度=最大匹配个数*2/(len_a+len_b)

因为a和b的最开始匹配的部位都是任意的,所以枚举它们最开始匹配的部位即可。复杂度O(len_a*len_b*(k))k<=min(len_a,len_b);

其实这个穿应该都不是很长(不然这么挫的办法不可能0MS过。。)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define ll long long
#define maxn 360
#define pp pair<int,int>
#define INF 0x3f3f3f3f
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
char s[100001],t[100001];
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
void solve()
{
	int lens=strlen(s),lent=strlen(t),ans=-INF;
	for(int i=0;i<lens;i++)
	{

		for(int j=0;j<lent;j++)
		{
			int cnt=0;
			for(int k=i,m=j;k<lens&&m<lent;k++,m++)
				if(s[k]==t[m])
				cnt++;
			ans=max(ans,cnt);
		}
	}
	ans*=2;int tem=gcd(ans,lens+lent);
	if(ans%(lens+lent)==0)
	printf("appx(%s,%s) = %d\n",s,t,ans/(lens+lent));
	else
	printf("appx(%s,%s) = %d/%d\n",s,t,(ans/tem),(lens+lent)/tem);
}
int main()
{
	while(~scanf("%s",s)&&s[0]!='-')
	{
		scanf("%s",t);
        solve();
	}
	return 0;
}
时间: 2024-10-12 18:38:35

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 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(sca

[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

[ACM] POJ 1753 Flip Game (枚举,BFS,位运算)

Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29921   Accepted: 12975 Description Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 squares. One side of each piece is white and the

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

String Matching -- Brute Force + Rabin-Karp + KMP

String Matching 这个问题已经被做烂了... 下面是C语言实现集合. http://www-igm.univ-mlv.fr/~lecroq/string/ 留个爪- 暴力解法: 暴力美啊- """ Programmer : EOF Date : 2015.02.28 Code file : nsm.py """ def naive_string_matcher(T, P) : if (T or P) is None : return

HDU1306 String Matching 【暴力】

String Matching Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 847    Accepted Submission(s): 434 Problem Description It's easy to tell if two words are identical - just check the letters. But

九度OJ 1094 String Matching

题目1094:String Matching 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:587 题目描述: Finding all occurrences of a pattern in a text is a problem that arises frequently in text-editing programs. Typically,the text is a document being edited,and the pattern searched

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

NYOJ5 Binary String Matching

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