Same String

本文介绍了一种使用最短路径算法解决字符串转换问题的方法,通过构建字符间的转换路径,判断两个字符串是否可以通过预设的转换规则相互转换。文章详细展示了算法的实现过程,包括初始化字符转换矩阵、读取输入字符串、设定转换规则以及最终判断字符串转换的可行性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以用最短路做,想通了就很简单了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <string.h>
#define INF 0x3f3f3f3f
#define _for1(i,a,b) for( int i=(a); i<(b); ++i)
#define _for2(i,a,b) for( int i=(a); i>(b); i--)
#define _rep1(i,a,b) for( int i=(a); i<=(b); ++i)
#define _rep2(i,a,b) for( int i=(a); i>=(b); i--)
typedef long long ll;
using namespace std;
#define maxn 105
int n, nn;
int m[300][300];
char s1[maxn], s2[maxn];

int main() 
{
	memset(m, 0,sizeof(m));
	for (char i = 'a'; i <= 'z'; i++) {
		m[i][i] = 1;
	}
	cin >> n; 
	getchar();
	cin >> s1 >> s2; 
	getchar();
	cin >> nn; 
	getchar();
	_for1(i,0, nn) {
		char cf, ct;
		scanf("%c %c", &cf, &ct);
		getchar();
		m[cf][ct] = 1;
	}
	for (char k = 'a'; k <= 'z'; k++) {
		for (char i = 'a'; i <= 'z'; i++) {
			for (char j = 'a'; j <= 'z'; j++) {
				if (m[i][k] && m[k][j]) {
					m[i][j] = 1;
				}
			}
		}
	}
	int f = 1;
	_for1(i,0, n) {
		if (!m[s1[i]][s2[i]]) {
			f = 0;
			break;
		}
	}
	printf("%s", f ? "YES\n" : "NO\n");
	return 0;
}

 

Objectives of this Assignment 1. Declare arrays of different types dynamically. 2. Iterate through arrays, processing all of the elements. 3. Write methods with arrays as parameters and return values In this assignment you will create your own class and write the methods in it. The class you write is called Main and it has four methods that build arrays and four methods that process arrays. All methods should be public and static. Create a project called P7_3 and a class named Main in a file calledMain.java, then follow the instructions below exactly: 1. Write a method named createStrings that takes a String as an input parameter, and returns an array of string with 4 elements. The first element is the original string passed in, the second element is the same string converted to uppercase, the third element is the same string converted to lowercase, and the fourth element is the same string with the first character removed. 2. Write a method called charFrequency that takes an array of strings and a single character as parameters. The method should iterate the array, counting instances of the character passed in. For example, if the array is ["Hello", "There"] and we are asked to count the character 'e', the return value will be three. If the character is not found in any of the elements in the array, the return value should be zero. 3.Add a main method with the usual signature that instantiates the Main class and tests its methods as follow: public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); char ch = in.next().charAt(0); // Create arrays String[] stringArray = createStrings(str); // Test processing System.out.println(charFrequency(stringArray, ch)); in.close(); } Input Specification: There are two lines in the input. The first line is the string to be converted. The second line contains one character to be counted. Output Specification: For each case, out
最新发布
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值