hdu 5414 CRB and String 2015多校聯合訓練賽#10 貪心

來源:互聯網
上載者:User

CRB and String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 246    Accepted Submission(s): 90


Problem Description CRB has two strings  s and  t.
In each step, CRB can select arbitrary character  c of  s and insert any character  d ( d ≠ c) just after it.
CRB 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 ≤  105
1 ≤  |s| ≤  |t| ≤  105
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 CRB can convert s to t, otherwise output "No".  
Sample Input

 4 a b cat cats do do apple aapple   

Sample Output
 No Yes Yes No   

Author KUT(DPRK)  
Source 2015 Multi-University Training Contest 10
首先保證首碼要一樣,然後逆序貪心做,能匹配就匹配,不然就添加。

最後查看是否全部匹配成功了,如果還有t中還有字元,判段最右邊的是否跟開頭一樣就行



#include <iostream>#include <algorithm>#include <cstring>#include <cstdio>#define maxn 100007using namespace std;char s[maxn],t[maxn];int lef1[maxn],lef2[maxn];int main(){    int tt;    scanf("%d",&tt);    while(tt--){        scanf("%s",s);        scanf("%s",t);        int l1 = strlen(s), l2 = strlen(t);        int flag = 1;        if(s[0] != t[0] || l1 > l2)            flag = 0;        int i = l1-1,j=l2-1,c1=1,c2=1;        while(s[c1] == t[c2] && s[c1] == s[0]&& c1 < l1 && flag)c1++,c2++;        for(; j >= c1; j--){            if(i == c2-1) break;            if(t[j] ==  s[i] && i > 0)i--;        }        if(i > c2-1) flag = 0;        if(s[c1-1] == t[c1])flag = 0;        if(flag)puts("Yes");        else puts("No");    }    return 0;}/*40abcatcatsdodoappleaappleaaaaaaaaaaaattaaaaaattaaaaappleallppleaaaabaaaaaaaaaaabaaaaaaaaaabaaa*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.