posts - 17,  comments - 0,  trackbacks - 0
 1 #include <cstdlib>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 
 7 int main(int argc, char *argv[])
 8 {
 9     char a[30], b[30];
10     int i,j,k,m;
11     cin>>a>>b;
12     k=0;
13     while(b[k]!='\0')
14         k++;
15     //cout<<"k="<<k<<endl;
16     i=0;j=0;m=0;
17     while(a[i]!='\0')
18     {
19         //cout<<i<<endl;
20         if((a[i]==b[j])&&b[j]!='\0')
21         {
22            i++;j++;m++;
23         }
24         else
25         {
26             i=i-m+1;j=0;m=0;
27         }
28         
29         if(j==k)
30         {
31             cout<<(i-k)<<endl;
32             break;
33         }
34     }
35     if(j!=k)
36         cout<<"-1"<<endl;
37     
38     system("PAUSE");
39     return EXIT_SUCCESS;
40 }
41 
COJ 1073 相对改进了些
 1 #include <cstdlib>
 2 #include <iostream>
 3 using namespace std;
 4 
 5 char a[101], b[101];
 6 int main(int argc, char *argv[])
 7 {
 8     int n,i,j,k; 
 9     
10     scanf("%s%s",a,b);
11     i=0;j=0;k=0;
12     while(b[i]!='\0')
13     {
14         
15         if((b[i]==a[j])&&(a[j]!='\0'))
16         {
17             i++;j++;k++;
18         }
19         else
20         {
21             i=i-k+1;j=0;k=0;
22         }        
23         if(a[j]=='\0')
24         {
25             cout<<"YES"<<endl;
26             break;
27         }
28         
29     }
30     if(a[j]!='\0')
31     {
32         cout<<"NO"<<endl;
33     }
34 
35     system("PAUSE");
36     return EXIT_SUCCESS;
37 }
38 
COJ 1074 
 1 #include <cstdlib>
 2 #include <iostream>
 3 
 4 using namespace std;
 5 
 6 
 7 int main(int argc, char *argv[])
 8 {
 9     int n,i,j,k;
10     char s[100],t[100];
11     scanf("%s%s",s,t);
12     i=0;j=0;k=0;
13     while(s[i]!='\0')
14     {
15         if(t[j]!='\0')
16         {
17             if(s[i]==t[j])
18             {
19                 i++;j++;k++;
20             }
21             else
22             {
23                 i=i-k+1;j=0;k=0;
24             }              
25         }  
26         else//(t[j]=='\0')
27         {
28             printf("%d\n",i-k);
29             break;
30         }
31     }
32     
33 
34     system("PAUSE");
35     return EXIT_SUCCESS;
36 }
37 
posted on 2011-11-07 19:50 刘聪 阅读(278) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

常用链接

留言簿(4)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜