注册 登录
编程论坛 数据结构与算法

一个小程序,帮忙简化,谢谢

athenaliu 发布于 2010-04-15 13:37, 596 次点击
#include<stdio.h>

main()
{
    int t[28]={'a','b','a','b','c','a','b','c','a','c','b','a','b','c','a','c','b','b','a','a','c','c','a','b','c','c','a','a'};
    int p[5]={'a','b','c','a','c'};
    int i,j=0;
    for(i=0;i<=23;i++)
    {
        if(t[i]=p[j])
        {
            if(j<=4)
                j++;
            else
            {
                printf("t include p\n");
                printf("i=%d\n",i);
                break;
            }
        }
        else
            printf("t do not include p/n");
    }

}            
7 回复
#2
linjx01232010-04-15 22:39
没看懂程序要表达什么

 for(i=0;i<=23;i++)这个是不是i<=28呢

反正按照你的思路,可以这么优化下
#include<stdio.h>

main()
{
    int t[28]={'a','b','a','b','c','a','b','c','a','c','b','a','b','c','a','c','b','b','a','a','c','c','a','b','c','c','a','a'};
    int p[5]={'a','b','c','a','c'};
    int i,j=0,k=4;
    int flag=0;
    for(i=0;i<=23/2;i++)
    {
        if(j>k){
                printf("t include p\n");
                printf("i=%d\n",i);
                flag=1;
                break;
            }
        if(t[i]==p[j])
        {         
            j++;
        }
        if(t[23-i]==p[k]){
            k--;
        }
    }
    if(flag==0)
        printf("t do not include p/n");
}            
#3
athenaliu2010-04-16 16:04
是看两个数组是否能匹配的
#4
lovebeba2010-04-16 16:49
- - 看不懂
#5
AI恒子2010-04-16 22:00
你要干嘛·  
#6
bloodthirsty2010-04-17 03:01
不知道,这样是你想要的么,,不过我是用.net写的.
       private static int START_INDEX = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            char[] t={'a','b','a','b','c','a','b','c','a','c','b','a','b','c','a','c','b','b','a','a','c','c','a','b','c','c','a','a'};
            char[] p = { 'a', 'b', 'c', 'a', 'c' };

            int result = 0;
            int total = t.Length -1;
            bool val = false;
            
            Compare(t, p, 0, 0, ref result, ref total);

            if (result == 5)
            {
                val = true;
            }

            MessageBox.Show(val.ToString() + " StartIndex: " + START_INDEX.ToString());
        }

        private void Compare(char[] t, char[] p, int index, int target, ref int result, ref int total)
        {
            if (index == total) return;
            if (result == 5) return;
            

            if (t[index] == p[target])
            {
                if (START_INDEX == 0)
                    START_INDEX = index;
               
                result = result + 1;
                Compare(t, p, index + 1, target + 1, ref result , ref total);
            }
            else
            {
                START_INDEX = 0;
                result = 0;
                Compare(t, p, index + 1, 0, ref result, ref total);
            }
        }
#7
寒风中的细雨2010-04-17 03:59
#include<stdio.h>

int main()
{
    char t[23]={'a','b','a','b','c','a','b','c','a','c','b','a','b','c','a','c','b','b','a','a','c','c'};
    char p[5]={'a','b','c','a','c'};
    int i=0, j=0, k;
    for(i=0; i<23; i++)
    {
        k = i;
        while( i<23 && j<5 && t[i]==p[j] )
        {
            i++;
            j++;
        }
        if( j==5 )
        {
            printf("t include p\n");
            printf("%d\n", k+1);
            return 0;
        }
        if( i==23 )
            printf("t do not include p/n");
        else
        {
            i = k++;
            j = 0;
        }
    }
    return 0;
}
#8
athenaliu2010-04-17 10:43
谢谢
1