请教一道简单的ACM题目

XiaoG602 2009-04-18 03:47:05
Description

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.
Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.


Input

Input Specification
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.


Output

Output Specification
For each test case output Yes, if s is a subsequence of t.


Sample Input


sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter


Sample Output


Yes
No
Yes
No

以下是我的代码
#include <iostream> 
#include <cstring>
using namespace std;

int main()
{
char s[255] = {'\0'}, t[255] = {'\0'};
int p1,p2,l1,l2;
while(cin >> s,cin>>t)
{
l1=strlen(s);
l2=strlen(t);
p1=p2=0;

while(p1!=l1&&p2!=l2)
{
while(s[p1]!=t[p2])
{
p2++;
if(p2>=l2)
break;
}
p1++;
}
if(p1!=l1||p2>=l2)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
return 0;
}

为什么在学校的检测系统上,总是runtime error。我感觉是 Input is terminated by EOF。这个要求没做到,但不知道应该怎么弄,谢谢大家乐
...全文
302 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ltc_mouse 2009-04-18
  • 打赏
  • 举报
回复
Sorry,说错了,是Ctrl+Z,回复后就感觉咋不对劲呢,呵呵,Ctrl+C是强行退出...
lin12345 2009-04-18
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
#include<fstream>


using namespace std;

int main() {
ifstream in;
in.open("input");
if( !in ) {
cerr<<"no such file"<<endl;
system("pause");
return 0;
}

string s,t;
while( in>>s>>t ) {
int len1(s.size());
int len2(t.size());

int i(0),j(0);
while( i<len1&&j<len2 ) {
if( s[i]==t[j] )
++i;

if( i==len1 )
break;

++j;
}

if( i==len1 )
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}

system("pause");
return 0;
}



采用文件流就可以了
ltc_mouse 2009-04-18
  • 打赏
  • 举报
回复
为什么在学校的检测系统上,总是runtime error。我感觉是 Input is terminated by EOF。这个要求没做到,但不知道应该怎么弄,谢谢大家乐
-----------------
键盘输入的话,Ctrl+C就是EOF了;ACM会把标准输入重定向为文件,写程序时就按照键盘输入的写就行了~

runtime error是否是对某个特殊的测试例,发生了越界问题呢?
把s,t数组大小设定为255是什么原因呢?似乎题目中看不到这个限制呀~~

对于不定长的,也许用string类来处理好一点~
liubuweiright 2009-04-18
  • 打赏
  • 举报
回复
学习...
baiwei156 2009-04-18
  • 打赏
  • 举报
回复
加入流读取以后程序如下:



#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

int main()
{
char s[255] = {'\0'}, t[255] = {'\0'};
int p1,p2,l1,l2;
p1=p2=l1=l2=0;

ifstream infile("E:\\myfile.txt");
while(infile >> s,infile>>t)
{
l1=strlen(s);
l2=strlen(t);
p1=p2=0;

while(p1!=l1&&p2!=l2)
{
while(s[p1]!=t[p2])
{
p2++;
if(p2>=l2)
break;
}
p1++;
}
if(p1!=l1||p2>=l2)
cout<<"No"<<endl;
else
cout<<"Yes"<<endl;
}
return 0;
}



文件内容:
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

输出结果:
Yes
No
Yes
No


完全复合题目的要求了
  • 打赏
  • 举报
回复

int main()
{
char s[255] = {'\0'}, t[255] = {'\0'};
int p1,p2,l1,l2;
while(cin >> s,cin>>t)
{
int len_s_ok=0;
l1=strlen(s);
l2=strlen(t);
p1=p2=0;
for(int i=0;i<l2;i++)
{
if(s[len_s_ok]==t[i])
{
len_s_ok++;
if(len_s_ok==l1)
break;
}
}
if(len_s_ok==l1)
printf("Yes\n");
else
printf("No\n");

}
return 0;
}


如果我没理解错意思的话,这样就够了
  • 打赏
  • 举报
回复
if you can remove characters from t such that the concatenation of the remaining characters is s.



最后这句其实还把要求降低了,不需要查找子串了.你上面的代码提交超时了?我写个看下.

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧