A Secret()

A Secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 256000/256000 K (Java/Others)
Total Submission(s): 2423    Accepted Submission(s): 892


Problem Description
Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,which have a big secret.SF is interested in this secret and ask VS how to get it.There are the things that VS tell:
  Suffix(S2,i) = S2[i...len].Ni is the times that Suffix(S2,i) occurs in S1 and Li is the length of Suffix(S2,i).Then the secret is the sum of the product of Ni and Li.
  Now SF wants you to help him find the secret.The answer may be very large, so the answer should mod 1000000007.
 

Input
Input contains multiple cases.
  The first line contains an integer T,the number of cases.Then following T cases.
  Each test case contains two lines.The first line contains a string S1.The second line contains a string S2.
  1<=T<=10.1<=|S1|,|S2|<=1e6.S1 and S2 only consist of lowercase ,uppercase letter.
 

Output
For each test case,output a single line containing a integer,the answer of test case.
  The answer may be very large, so the answer should mod 1e9+7.
 

Sample Input
 
 
2 aaaaa aa abababab aba
 

Sample Output
 
 
13 19
Hint
case 2: Suffix(S2,1) = "aba", Suffix(S2,2) = "ba", Suffix(S2,3) = "a". N1 = 3, N2 = 3, N3 = 4. L1 = 3, L2 = 2, L3 = 1. ans = (3*3+3*2+4*1)%1000000007.

  
  
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cstring>
  4. #include<cstdio>
  5. using namespace std;
  6. const int LEN= 1e6+ 8;
  7. const int mod= 1e9+ 7;
  8. long long kmp_next[LEN],ans[LEN];
  9. char x1[LEN],x2[LEN];
  10. void KMP_next(char *a)
  11. {
  12. int i= 0,j= -1;
  13. kmp_next[ 0]= -1;
  14. while(a[i])
  15. if(j== -1||a[i]==a[j])
  16. kmp_next[++i]=++j;
  17. else
  18. j=kmp_next[j];
  19. }
  20. void KMP(char *a,char *b)
  21. {
  22. int i= 0,j= 0,m= strlen(b);
  23. while(a[i])
  24. {
  25. if(j== -1||a[i]==b[j])
  26. ++i,++j,ans[j]++;
  27. else
  28. j=kmp_next[j];
  29. if(j==m)
  30. j=kmp_next[j];
  31. }
  32. }
  33. int main()
  34. {
  35. int N;
  36. cin>>N;
  37. while(N--)
  38. {
  39. scanf( "%s%s",x1,x2);
  40. int n= strlen(x1),m= strlen(x2);
  41. reverse(x1, x1 + strlen(x1));
  42. reverse(x2, x2 + strlen(x2));
  43. memset(ans, 0, sizeof(ans));
  44. KMP_next(x2);
  45. KMP(x1,x2);
  46. long long ANS= 0;
  47. for( int i=m; i> 0; i--)
  48. ans[kmp_next[i]]+=ans[i];
  49. for( int i= 1; i<=m; i++)
  50. ANS=(ANS+ans[i]*i)%mod;
  51. printf( "%lld\n",ANS);
  52. }
  53. return 0;
  54. }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, I can help you with that. Here is the solution to your problem: GuessingGame.java ``` public class GuessingGame { private String secretWord; private StringBuilder currentGuess; private int remainingAttempts; public GuessingGame(String word) { secretWord = word; currentGuess = new StringBuilder("*".repeat(word.length())); remainingAttempts = 10; } public String getCurrentGuess() { return currentGuess.toString(); } public int getRemainingAttempts() { return remainingAttempts; } public boolean guessLetter(char letter) { boolean correctGuess = false; for (int i = 0; i < secretWord.length(); i++) { if (secretWord.charAt(i) == letter) { currentGuess.setCharAt(i, letter); correctGuess = true; } } if (!correctGuess) { remainingAttempts--; } return correctGuess; } public boolean isGameWon() { return currentGuess.indexOf("*") == -1; } public boolean isGameOver() { return remainingAttempts == 0; } } ``` Main.java ``` import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a secret word: "); String secretWord = scanner.nextLine(); GuessingGame game = new GuessingGame(secretWord); while (!game.isGameOver()) { System.out.println("Current guess: " + game.getCurrentGuess()); System.out.println("Remaining attempts: " + game.getRemainingAttempts()); System.out.print("Enter a letter: "); char letter = scanner.nextLine().charAt(0); if (game.guessLetter(letter)) { System.out.println("Correct guess!"); if (game.isGameWon()) { System.out.println("You win!"); return; } } else { System.out.println("Incorrect guess!"); } } System.out.println("Game over! The secret word was: " + secretWord); } } ``` The `GuessingGame` class represents the game logic. It stores the secret word, the current guess, and the remaining attempts. It also provides methods to make a guess, check if the game is won or over, and retrieve the current guess and remaining attempts. The `Main` class is the entry point of the application. It prompts the user for a secret word, creates a `GuessingGame` instance, and starts a loop that allows the user to make guesses until the game is won or over. Each iteration of the loop displays the current guess, the remaining attempts, and prompts the user for a letter. It then calls the `guessLetter` method of the `GuessingGame` instance and displays a message depending on whether the guess was correct or not. If the game is won or over, it displays an appropriate message and exits the loop. Note that the `Main` class assumes that the user enters a valid input (i.e., a single letter) and doesn't handle any exceptions. You may want to add error handling or input validation code to make the application more robust.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值