如何将字符串的子串反转,用于Samara大学生程序设计竞赛问题E?

2026-04-11 22:002阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计547个文字,预计阅读时间需要3分钟。

如何将字符串的子串反转,用于Samara大学生程序设计竞赛问题E?

给定两个长度相同的字符串s和t,判断是否可以通过对s中某个子串进行一次反转,恰好得到t。输入:第一行包含字符串s,第二行包含字符串t。

Two strings s and t of the same length are given. Determine whether it is possible to make t from s using
exactly one reverse of some its substring.
Input
The first line contains the string s, and the second — the string t. Both strings have the same length from
1 to 200000 characters and consist of lowercase Latin letters.
Output
Output «YES», if it is possible to reverse some substring of s to make s equal to t, and «NO», otherwise.
Examples
standard input standard output
abcdefg
abedcfg
YES
abcdefg
abdecfg
NO

#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<string> #include<bitset> #include<ctime> #include<deque> typedef long long ll; using namespace std; typedef unsigned long long int ull; #define maxn 500005 #define ms(x) memset(x,0,sizeof(x)) #define Inf 0x7fffffff #define inf 0x3f3f3f3f const long long int mod = 1e9 + 7; #define pi acos(-1.0) #define pii pair<int,int> #define eps 1e-7 #define pll pair<ll,ll> ll quickpow(ll a, ll b) { ll ans = 1; a = a % mod; while (b > 0) { if (b % 2)ans = ans * a; b = b / 2; a = a * a; } return ans; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } bool prime(int x) { if (x == 0 || x == 1)return false; for (int i = 2; i <= sqrt(x); i++) { if (x%i == 0)return false; } return true; } int main() { ios::sync_with_stdio(false); string s, t; cin >> s >> t; int i, j; int len = s.length(); int pos1, pos2; if (s == t) { int flag = 0; cout << "YES" << endl; } else { for (i = 0; i < len; i++) { if (s[i] != t[i]) { pos1 = i; break; } } for (i = len - 1; i >= 0; i--) { if (s[i] != t[i]) { pos2 = i; break; } } int pp1 = pos1, pp2 = pos2; int flag = 0; while (1) { if (s[pos1] != t[pos2]) { flag = 1; break; } pos1++; pos2--; if (pos1 > pp2)break; } if (flag)cout << "NO" << endl; else cout << "YES" << endl; } }

如何将字符串的子串反转,用于Samara大学生程序设计竞赛问题E?

本文共计547个文字,预计阅读时间需要3分钟。

如何将字符串的子串反转,用于Samara大学生程序设计竞赛问题E?

给定两个长度相同的字符串s和t,判断是否可以通过对s中某个子串进行一次反转,恰好得到t。输入:第一行包含字符串s,第二行包含字符串t。

Two strings s and t of the same length are given. Determine whether it is possible to make t from s using
exactly one reverse of some its substring.
Input
The first line contains the string s, and the second — the string t. Both strings have the same length from
1 to 200000 characters and consist of lowercase Latin letters.
Output
Output «YES», if it is possible to reverse some substring of s to make s equal to t, and «NO», otherwise.
Examples
standard input standard output
abcdefg
abedcfg
YES
abcdefg
abdecfg
NO

#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<string> #include<bitset> #include<ctime> #include<deque> typedef long long ll; using namespace std; typedef unsigned long long int ull; #define maxn 500005 #define ms(x) memset(x,0,sizeof(x)) #define Inf 0x7fffffff #define inf 0x3f3f3f3f const long long int mod = 1e9 + 7; #define pi acos(-1.0) #define pii pair<int,int> #define eps 1e-7 #define pll pair<ll,ll> ll quickpow(ll a, ll b) { ll ans = 1; a = a % mod; while (b > 0) { if (b % 2)ans = ans * a; b = b / 2; a = a * a; } return ans; } int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); } bool prime(int x) { if (x == 0 || x == 1)return false; for (int i = 2; i <= sqrt(x); i++) { if (x%i == 0)return false; } return true; } int main() { ios::sync_with_stdio(false); string s, t; cin >> s >> t; int i, j; int len = s.length(); int pos1, pos2; if (s == t) { int flag = 0; cout << "YES" << endl; } else { for (i = 0; i < len; i++) { if (s[i] != t[i]) { pos1 = i; break; } } for (i = len - 1; i >= 0; i--) { if (s[i] != t[i]) { pos2 = i; break; } } int pp1 = pos1, pp2 = pos2; int flag = 0; while (1) { if (s[pos1] != t[pos2]) { flag = 1; break; } pos1++; pos2--; if (pos1 > pp2)break; } if (flag)cout << "NO" << endl; else cout << "YES" << endl; } }

如何将字符串的子串反转,用于Samara大学生程序设计竞赛问题E?