HDU 5414 字符串
来源:互联网 发布:配置java环境变量 编辑:程序博客网 时间:2024/06/16 19:20
HDU 5414
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5414
题意:
给两个字符串S,T,问能否把S转换成T。转换规则是,可以在S中已知字母后插入新字母,但是新字母不能与已知字母相同。
思路:
先说错误思路,比赛的时候构图构错,把图想成把所有的s元素拆开,然后往里面加缝隙加元素或者不加。应该是相同的连一块,不同的元素之间拆开。
错误
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
正确
这样就可以很轻易的发现,S和T的第一块是要完全匹配的,后面的只要T在遍历到结尾前找到一个一样的,怎样都好(原因的话因为总可以在同一个元素后面反复插入元素来避免出现插入不合法的情况)。
源码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
const int MAXN = 1e5 + 5;
char str1[MAXN], str2[MAXN];
bool solve()
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int i, j;
for(j = 0 ; j < len2 ; j++)
if(str2[j] != str2[0])
break;
for(i = 0 ; i < j ; i++)
if(str1[i] != str2[i]){
// printf("j = %d\n", j);
return false;
}
for(; i < len1 ; i++){
while(j < len2 && str1[i] != str2[j])
j++;
if(j == len2){
// printf("i = %d\n", i);
return false;
}
}
return true;
}
int main()
{
int t;
scanf("%d", &t);
while(t--){
scanf("%s%s", str1, str2);
int ok = solve();
if(ok)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
- HDU 5414 字符串
- hdu字符串
- hdu 5414 CRB and String(字符串模拟)
- hdu 5414 CRB and String(字符串模拟)
- hdu 2017 字符串统计
- HDU 1274 展开字符串
- 【字符串暴力】hdu 2847
- HDU-1039 字符串处理
- HDU 1048 字符串
- hdu 1020 简单字符串
- HDU 1274(展开字符串)
- HDU 2017 字符串统计
- HDU 1274 展开字符串
- hdu 1062 字符串处理
- hdu 1088 字符串
- HDU 2100 字符串
- HDU 1238 字符串
- HDU 1200 字符串
- JAVA_SE基础——27.匿名对象
- [CQOI2007]三角形tri 解题报告
- nginx中的try_files指令解释
- SGU 242(MaxFlow)
- “垃圾围城”怎么破? 盘点国外垃圾处理经验
- HDU 5414 字符串
- uva1584_水题(环状串的处理)
- mysql安装 编码设置 远程访问
- HDOJ 2041 超级楼梯 (打表法)
- HDOJ 2199 Can you solve this equation? (二分法)
- ios线程-创建及通信
- 事件
- 微信 开发 总结
- hdu 4300 Clairewd’s message