uva10739(String to Palindrome)

1998 단어 Stringuva기초
사고방식: 제목은 문자열을 정한 다음에 각 문자를 한 번씩 수정할 수 있다는 것이다. 최소한 얼마나 수정해야 문자열을 회문 문자열로 바꿀 수 있는지 물어보는 것이다.이 문제의 상태는 분명히 매우 비슷하다. dp[i][j]는 구간 [i, j]의 문자열을 회문으로 수정하는 데 필요한 최소한의 횟수를 표시하고 그 다음에 방정식을 옮긴다.
1,s[i] = s[j],dp[i][j] = dp[i + 1][j - 1];
2, s[i] != s[j], 분명히 이 일은 한 번 수정해야 한다. s[i]를 수정하는지 s[j]를 수정하는지 s[i]를 수정하면 dp[i+1][j], s[j]를 수정하면 dp[i][j-1]이고 물론 d[i+1][j-1]도 있다.최소자 + 1을 취하기;
/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
inline int Readint(){
	char c = getchar();
	while(!isdigit(c)) c = getchar();
	int x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x;
}
char s[1010];
int dp[1010][1010];
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t,icase = 0;
	scanf("%d",&t);
	while(t--){
		scanf("%s",s);
		int r = strlen(s);
		memset(dp, 0,sizeof dp);
		for (int i = r - 1;i >= 0;--i){
			for (int j = i + 1;j <= r - 1;++j){
				if (s[i] == s[j]) dp[i][j] = dp[i + 1][j - 1];
				else dp[i][j] = min(min(dp[i + 1][j],dp[i][j - 1]),dp[i + 1][j - 1]) + 1;
			}
		}
		printf("Case %d: %d
",++icase,dp[0][r - 1]); } return 0; }

좋은 웹페이지 즐겨찾기