QDU-GZS and String
Description
?
GZS has two strings?s?and?t.
In each step, GZS can select arbitrary character?c?of?s?and insert any character?d?(d?≠?c) just after it.
GZS wants to convert?s?to?t. But is it possible?
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case there are two strings s and t, one per line. 1 ≤ T ≤ 10^5 1 ≤ |s| ≤ |t| ≤ 10^5 All strings consist only of lowercase English letters. The size of each input file will be less than 5MB.
Output
For each test case, output "Yes" if GZS can convert s to t, otherwise output "No".
Sample Input 1?
4 a b cat cats do do apple aappleSample Output 1
No Yes Yes No題意:一開始讀錯題了,以為只能插一個,是可以插很多個,每個要滿足插入的規則就可以了
思路:看長串中是否包含短串的所有字符,如果都有再判斷前面有多少個相同的,如果短串的大于等于長串的,并且首字符一定要相同,就可以,否則就不可以,否則就說明插了重的
代碼:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #define MAX 100005 using namespace std;char s1[MAX],s2[MAX]; int main() {int T;cin>>T;for(int t=0; t<T; t++) {scanf("%s",s1);scanf("%s",s2);int i;i=0;int len1=strlen(s1);int len2=strlen(s2);for(int j=0; i<len1&&j<len2; j++) {if(s1[i]==s2[j]) {i++;}}if(i!=len1) {printf("No\n");} else {int sum1=0,sum2=0;for(int j=1; j<len1; j++) {if(s1[j]==s1[j-1]) {sum1++;} else {break;}}for(int j=1; j<len2; j++) {if(s2[j]==s2[j-1]) {sum2++;} else {break;}}if(s1[0]==s2[0]&&sum1>=sum2) {printf("Yes\n");} else {printf("No\n");}}}return 0; }也可以參考大佬的思路及代碼
鏈接:
https://blog.csdn.net/qq_42936517/article/details/85886959
轉載于:https://www.cnblogs.com/Staceyacm/p/10781859.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的QDU-GZS and String的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JSON数据格式:以及XML文件格式,Y
- 下一篇: Mybatis入门及于hibernate