Powerful Incantation(HDU 4150)
来源:互联网 发布:安卓手机淘宝网 编辑:程序博客网 时间:2024/06/08 06:27
Powerful Incantation
Time Limit: 2000 MSMemory Limit: 32768 KB
64-bit integer IO format: %I64d , %I64u Java class name: Main
Description
Some dangerous Witches invaded the garden city! As a righteous magician, james0zan want to find the most powerful incantation so he can beat those dangerous witches easily.
After consulted one hundred and eight thousand Magic Books, james0zan find a way to calculate the power of an incantation. There is a very short incantation called “magic index” which contains the magic power, and each incantation’s power can be calculated by the times the “magic index” appearance in the incantation. Notice that if two “magic index” overlapped, the power only should be calculated once. And we just want the incantation more powerful. That is to say, if the “magic index” is “aa”, the power of incantation “aaaa” is 2(“aa”+”aa”), not 1(“a”+”aa”+”a”) or 3.
After consulted one hundred and eight thousand Magic Books, james0zan find a way to calculate the power of an incantation. There is a very short incantation called “magic index” which contains the magic power, and each incantation’s power can be calculated by the times the “magic index” appearance in the incantation. Notice that if two “magic index” overlapped, the power only should be calculated once. And we just want the incantation more powerful. That is to say, if the “magic index” is “aa”, the power of incantation “aaaa” is 2(“aa”+”aa”), not 1(“a”+”aa”+”a”) or 3.
Input
The first line is an integer t (t<=30), the number of test cases. Each of the next t lines contains two strings, represent the incantation (length<=10^6) and the “magic index” (length<=5). Every char in the incantation and the magic index is lowercase.
Output
For each test case you should output the power of the incantation.
Sample Input
3aaaa aabsdjfassdiifo sdpapapapapapapap ap
Sample Output
227
代码:
#include<stdio.h>
#include<string.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
char a[2000000],b[20]; //这里数组要开的足够大
scanf("%s%s",a,b);
int sum=0; //记录个数
int la=strlen(a);
int lb=strlen(b);
for(int i=0;i<la;i++)
{
int t=0; //标记变量,之所以要在这里定义是因为每次a的数组要循环
for(int j=0;j<lb;j++)
{
if(a[i+j]!=b[j]) //这里检测是否是子串
{
t=1;
break;
}
}
if(t==0)
{
sum++;
i=i+lb-1; //这里这个减一极其极其重要,因为如果不加一就会跳过一个字符,因为后边有i++
}
}
printf("%d\n",sum);
}
}
0 0
- HDU 4150 Powerful Incantation
- HDU 4150 Powerful Incantation
- hdu 4150 Powerful Incantation
- hdu 4150 Powerful Incantation
- Powerful Incantation(HDU 4150)
- hdu 4150 Powerful Incantation 暴力||KMP
- 【HDU 4150】Powerful Incantation —— 字符串匹配
- HDU 4150 Powerful Incantation / Codeforces 625B - War of the Corporations
- 【字符串】hdu4150 Powerful Incantation
- 17.04.29 Powerful Incantation
- 2011百校联动“菜鸟杯”程序设计公开赛 &&Powerful Incantation
- HDU 3593 The most powerful force
- hdu-3593-The most powerful force
- HDU 3593 The most powerful force 树形DP(背包)
- hdu 3593 The most powerful force (树形dp+背包)
- HDU 3593The most powerful force 树状dp
- 【JZOJ4473】Incantation Solution
- Most Powerful
- 网络通信UDP(Linux/Windows)
- android 应用模式之mvp
- 2014 神奇算式
- delphi7的Application.ProcessMessages作用
- css伪类与伪元素
- Powerful Incantation(HDU 4150)
- 【bzoj1954】【The xor-longest Path】【trie树】
- jQuery学习笔记
- Application.ProcessMessages
- LeetCode 185. Department Top Three Salaries
- android studio 导入一个开源库文件汇总
- Execute & ExecuteGlobal
- R3多线程
- 观察者模式(这个用的太多了)